Asim Zaidi
Asim Zaidi

Reputation: 28344

Delaying execution with setTimeout

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); ?

Upvotes: 3

Views: 7714

Answers (3)

davydotcom
davydotcom

Reputation: 2210

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);
}

Upvotes: 11

Ben
Ben

Reputation: 867

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?

Upvotes: 5

Darin Dimitrov
Darin Dimitrov

Reputation: 1039428

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.

Upvotes: 3

Related Questions