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

 setInterval

One can think of many cases where being able to periodically execute a function would be useful. Polling a data source every few seconds and pushing updates is a common pattern. Running the next step in an animation every few milliseconds is another use case, as is collecting garbage. For these cases, setInterval is a good tool:

let intervalId = setInterval(() => { ... }, 100);

Every 100 milliseconds the sent callback function will execute, a process that can be cancelled with clearInterval(intervalReference).

Unfortunately, as with setTimeout, this behavior is not always reliable. Importantly, if a system delay (such as some badly written blocking while loop) occupies the event loop for some period of time, intervals set prior and completing within that interim will have their results queued on the stack. When the event loop becomes unblocked and unwinds, all the interval callbacks will be fired in sequence, essentially immediately, losing any sort of timing delays they intended.

Luckily, unlike browser-based JavaScript, intervals are rather more reliable in Node, generally able to maintain expected periodicity in normal use scenarios.

主站蜘蛛池模板: 揭阳市| 涟源市| 乌拉特后旗| 吐鲁番市| 平果县| 古蔺县| 渝北区| 温州市| 宽甸| 紫金县| 丹阳市| 秭归县| 定襄县| 确山县| 塘沽区| 东明县| 延安市| 北安市| 弥勒县| 同江市| 咸丰县| 荔波县| 乌拉特后旗| 蓬安县| 石景山区| 烟台市| 宁都县| 景泰县| 万全县| 乡城县| 毕节市| 茂名市| 洛宁县| 临沂市| 桓台县| 隆尧县| 图木舒克市| 兴隆县| 岚皋县| 濮阳市| 浑源县|