JavaScript's time out function is:
setTimeout(fun, 3600);
but what if I don't want to run any other function. Can I do setTimeout(3600);
?
JavaScript's time out function is:
setTimeout(fun, 3600);
but what if I don't want to run any other function. Can I do setTimeout(3600);
?
- 4 Huh if you aren't running another function what do you need the timeout for? – NullUserException Commented Sep 14, 2010 at 18:50
- 4 If nothing is to run at the end of the timer, why are you creating a timer at all? A little more context of your overall goal would be helpful here. – Nick Craver Commented Sep 14, 2010 at 18:50
- 3 If you're hoping to do this to make javascript "sleep", timeouts don't pause execution. – Daniel Vandersluis Commented Sep 14, 2010 at 18:54
- It's actually setTimeout(function, millis) – nickytonline Commented Sep 15, 2010 at 16:20
3 Answers
Reset to default 11Based on what you are saying you are simply trying to delay execution within a function.
Say for example you want to run an alert, and after 2 more seconds a second alert like so:
alert("Hello")
sleep
alert("World")
In javascript, the only 100% compatible way to accomplish this is to split the function.
function a()
{
alert("Hello")
setTimeout("b()",3000);
}
function b()
{
alert("World");
}
You can also declare the function within the setTimeout itself like so
function a()
{
alert("Hello");
setTimeout(function() {
alert("World");
},3000);
}
I'm not sure what you are trying to do. If you want nothing to happen after the period of time, why do you need a setTimeout()
in the first place?
You could always pass a handler which does nothing:
setTimeout(function() { }, 3600);
But I can hardly imagine any scenario in which this would be useful.