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

 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.

主站蜘蛛池模板: 本溪市| 商丘市| 金平| 兰考县| 冷水江市| 安龙县| 东莞市| 东方市| 兖州市| 岳阳市| 莱西市| 永州市| 南安市| 即墨市| 金塔县| 南木林县| 武穴市| 灵寿县| 泸州市| 永宁县| 鹤山市| 威宁| 开阳县| 隆回县| 吐鲁番市| 互助| 扎赉特旗| 墨江| 江源县| 通城县| 勐海县| 台湾省| 峨山| 布尔津县| 加查县| 内丘县| 柯坪县| 文安县| 淮安市| 仙桃市| 南通市|