views:

50

answers:

3

javascript time out function is

settimeout(fun, 3600);

but what if I dont want to run anyother function. can i do settimeout(3600); ?

+1  A: 

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.

Darin Dimitrov
+4  A: 

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?

path411
+4  A: 

Based 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);
}
davydotcom
`setTimeout()` takes a function *then* a delay, also try to avoid passing strings, just `setTimeout(b, 3000);` will do.
Nick Craver
or `setTimeout(function() { b(); }, 3000)` - for example... useful if you should want to use arguments. `setTimeout(function() { b(arg1, arg2); }, 3000)` .... also it's `setTimeout(code, delay)` not the other way around.
Peter Ajtai
@Peter - That won't work unless you change the `b()` function to *return* a function to be passed to the `setTimeout()`. You're right about the order of arguments, though. :o)
patrick dw
@patrick - What do you mean? It seems to work for me. [ **Here's an example.** ](http://jsfiddle.net/wVGKC/) ---- Alert triggered 2s after onLoad with arguments.
Peter Ajtai
@Peter - You're absolutely right. Somehow in my haste I didn't notice you were calling `b()` in an anonymous function. Don't know how I could have missed that! :o)
patrick dw
@patrick - That's the magic of closures and function scope (w regard to passing arguments) ;)
Peter Ajtai