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

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…])

主站蜘蛛池模板: 阳西县| 南澳县| 浦北县| 嵊泗县| 樟树市| 平南县| 来凤县| 贵州省| 永仁县| 渭南市| 安泽县| 科技| 肃宁县| 屯昌县| 南充市| 东源县| 上杭县| 凤阳县| 泾川县| 桂东县| 宿州市| 肃北| 蓝田县| 汕尾市| 枣强县| 卢氏县| 肥乡县| 阳朔县| 凉山| 苍南县| 朝阳市| 华蓥市| 抚顺市| 无棣县| 吴旗县| 常州市| 自贡市| 临武县| 东阳市| 普定县| 公主岭市|