- 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.
- 網絡云百問百答
- Cisco OSPF命令與配置手冊
- C++黑客編程揭秘與防范
- 物聯網(IoT)基礎:網絡技術+協議+用例
- 高校網絡道德教育研究
- Hands-On Industrial Internet of Things
- OpenLayers Cookbook
- 網絡互聯技術(實踐篇)
- 網絡的琴弦:玩轉IP看監控
- Socket.IO Real-time Web Application Development
- SSL VPN : Understanding, evaluating and planning secure, web/based remote access
- 區塊鏈輕松上手:原理、源碼、搭建與應用
- 6G新技術 新網絡 新通信
- Android UI Design
- 網絡AI+:2030后的未來網絡