- Mastering Node.js(Second Edition)
- Sandro Pasquali Kevin Faaborg
- 182字
- 2021-07-02 19:28:44
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.
- MERN Quick Start Guide
- 重新定義Spring Cloud實戰
- 面向物聯網的嵌入式系統開發:基于CC2530和STM32微處理器
- 網絡安全應急響應技術實戰
- 網管員必讀:網絡管理(第2版)
- 物聯網與智能家居
- Microsoft Power Platform Enterprise Architecture
- INSTANT LinkedIn Customization How-to
- 5G時代的大數據技術架構和關鍵技術詳解
- 精通SEO:100%網站流量提升密碼
- 現場綜合化網絡運營與維護:運營商數字化轉型技術與實踐
- 走近奇妙的物聯網
- 網絡信息安全工程技術與應用分析
- Building Microservices with Spring
- 視聽變革:廣電的新媒體戰略