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

 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.

主站蜘蛛池模板: 浦江县| 北海市| 潮安县| 汶上县| 饶河县| 洛扎县| 万年县| 外汇| 永兴县| 克山县| 富民县| 黄骅市| 讷河市| 桑植县| 凤庆县| 偏关县| 诏安县| 祁阳县| 察隅县| 丹江口市| 梁平县| 犍为县| 沾益县| 美姑县| 镇原县| 临沧市| 竹山县| 九江县| 乳山市| 保德县| 江川县| 横峰县| 藁城市| 城市| 乐陵市| 若羌县| 五指山市| 濮阳县| 恩平市| 外汇| 威远县|