开发者

Delaying execution with setTimeout

开发者 https://www.devze.com 2023-01-15 23:28 出处:网络
JavaScript\'s time out function is: setTimeout(fun, 3600); but what if I don\'t want to run any other fun开发者_StackOverflow中文版ction. Can I do setTimeout(3600); ?Based on what you are saying you

JavaScript's time out function is:

setTimeout(fun, 3600);

but what if I don't want to run any other fun开发者_StackOverflow中文版ction. Can I do setTimeout(3600); ?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消