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

javascript - setTimeout(fun) with a single argument? (Timeout not specified) - Stack Overflow

programmeradmin4浏览0评论

The HTML5 specifications states that setTimeout can be run without the additional "timeout" argument which is supposed to say after how many milliseconds will the function "handler" be scheduled.

handle = window . setTimeout( handler [, timeout [, arguments ] ] )
   Schedules a timeout to run handler after timeout milliseconds. Any arguments are passed straight through to the handler.

However, I failed to find anywhere which explains what happens when no "timeout" time period is set.

An example usage is, the animation implementation int the Raphael library.

animationElements[length] && win.setTimeout(animation);

The HTML5 specifications states that setTimeout can be run without the additional "timeout" argument which is supposed to say after how many milliseconds will the function "handler" be scheduled.

handle = window . setTimeout( handler [, timeout [, arguments ] ] )
   Schedules a timeout to run handler after timeout milliseconds. Any arguments are passed straight through to the handler.

However, I failed to find anywhere which explains what happens when no "timeout" time period is set.

An example usage is, the animation implementation int the Raphael library.

animationElements[length] && win.setTimeout(animation);
Share Improve this question edited Dec 11, 2011 at 7:54 user166390 asked Apr 27, 2010 at 17:48 Elazar LeibovichElazar Leibovich 33.6k36 gold badges127 silver badges174 bronze badges 5
  • All current major browsers are able to accept a single argument, so to avoid any confusion it's worth pointing out that a browser does not have to be HTML5 pliant to support this. – Andy E Commented Apr 27, 2010 at 18:09
  • 2 This is a splendid way of doing deferred procedure calls in JS - "do X later/not-right-now". If you're inside an event handler, it may be a bad time to call some function because of state/re-entrancy issues. Note that you cannot guarantee the actual execution time, but it's "soon". – JBRWilkinson Commented Apr 27, 2010 at 18:28
  • @Andy Mozilla MDC states the millisecond as a mandatory parameter. – Elazar Leibovich Commented Apr 27, 2010 at 18:34
  • @JBRWilkinson: Indeed, and it's also a great way to delay execution with the possibility of cancelling it if a particular event fires. I've used this technique numerous times and it's really useful. – Andy E Commented Apr 27, 2010 at 19:02
  • Related: Why is setTimeout(fn, 0) sometimes useful?, NodeJS - setTimeout(fn,0) vs setImmediate(fn). – Sebastian Simon Commented Sep 25, 2021 at 16:56
Add a ment  | 

1 Answer 1

Reset to default 17

See http://www.whatwg/specs/web-apps/current-work/multipage/timers.html#get-the-timeout

  1. Let timeout be the second argument to the method, or zero if the argument was omitted.
发布评论

评论列表(0)

  1. 暂无评论