官术网_书友最值得收藏!

Timers

Timers are used to schedule events in the future. They are used when one seeks to delay the execution of some block of code until a specified number of milliseconds have passed, to schedule periodic execution of a particular function, and so on.

JavaScript provides two asynchronous timers: setInterval() and setTimeout(). It is assumed that the reader is fully aware of how to set (and cancel) these timers, so very little time will be spent discussing the syntax. We'll instead focus more on gotchas and less well-known details about timeouts and intervals.

The key takeaway will be this: when using timers, one should make no assumptions about the amount of actual time that will expire before the callback registered for this timer fires, or about the ordering of callbacks. Node timers are not interrupts. Timers simply promise to execute as close as possible to the specified time (though never before), beholden, as with every other event source, to event loop scheduling.

At least one thing you may not know about timers-we are all familiar with the standard arguments to setTimeout: a callback function and timeout interval. Did you know that many additional arguments are passed to the callback function? setTimeout(callback, time, [passArg1, passArg2…])

主站蜘蛛池模板: 中方县| 江川县| 平和县| 陆川县| 龙州县| 安福县| 漳州市| 凤城市| 平谷区| 揭西县| 广昌县| 界首市| 虹口区| 吉木乃县| 乌拉特前旗| 云龙县| 张家界市| 莒南县| 南靖县| 曲松县| 卢湾区| 象州县| 大连市| 黔西县| 云安县| 沽源县| 孝感市| 镇远县| 屏边| 巴彦县| 罗定市| 施甸县| 同仁县| 贺兰县| 张家口市| 长宁区| 榆树市| 富锦市| 张家川| 黄山市| 云安县|