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

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

主站蜘蛛池模板: 乌苏市| 岗巴县| 顺昌县| 太原市| 清新县| 鄯善县| 石家庄市| 巫溪县| 仁寿县| 西乌珠穆沁旗| 武胜县| 灵寿县| 澄迈县| 长汀县| 原阳县| 从化市| 丽水市| 炎陵县| 尉犁县| 清镇市| 普陀区| 微山县| 阿尔山市| 晋宁县| 常德市| 长岭县| 蒙自县| 龙泉市| 仙桃市| 舞钢市| 郧西县| 融水| 平江县| 定安县| 淅川县| 平度市| 余江县| 固始县| 大足县| 炉霍县| 泰宁县|