最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Delaying execution with setTimeout - Stack Overflow

programmeradmin3浏览0评论

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

Share Improve this question edited Jan 7, 2023 at 14:29 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 14, 2010 at 18:48 Asim ZaidiAsim Zaidi 28.3k49 gold badges137 silver badges224 bronze badges 4
  • 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
Add a comment  | 

3 Answers 3

Reset to default 11

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)

  1. 暂无评论