can't seem to get this to work.
var current_times = new Date();
var future_times = new Date();
function time(){
current_times = current_times.setMinutes(current_times.getMinutes());
future_times = future_times.setMinutes(future_times.getMinutes() + 1);
}
error im getting is: current_times.getMinutes is not a function
note sure this helps, but the time function is called from a function which is initiated on body load.
-
2There doesn't seem to be anything wrong with this code. What browser are you using? As I anticipated, I cannot reproduce the problem in Chrome.kqnr– kqnr2011年04月12日 00:03:41 +00:00Commented Apr 12, 2011 at 0:03
-
11. The specific code you pasted works. 2. Don't setMinute to getMinutes(). Why would you do that?!Khez– Khez2011年04月12日 00:04:19 +00:00Commented Apr 12, 2011 at 0:04
-
1post a complete non-working code sample.Hamish– Hamish2011年04月12日 00:06:22 +00:00Commented Apr 12, 2011 at 0:06
2 Answers 2
The problem is that setMinutes
returns a number, not a Date
object.
The function will work the first time you call it, but on the second call current_times
and future_times
will be numbers, and hence won't have the getMinutes
function. Since setMinutes()
modifies the Date
object instead of producing a new one, the solution is to not reassign your variables.
Furthermore, if I understand your intentions correctly, your code can be simplified to:
var current_times, future_times = new Date();
function time() {
current_times = new Date();
future_times.setMinutes(current_times.getMinutes() + 1);
}
1 Comment
the code is right but as Box9 typed code
you must assign variables inside the function to be like that'
function time() {
var current_times = new Date();
var future_times = new Date();
current_times = current_times.setMinutes(current_times.getMinutes());
future_times = future_times.setMinutes(future_times.getMinutes() + 1);
document.write(current_times);
document.write("<br>"+future_times);
}