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

 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.

主站蜘蛛池模板: 宜宾县| 大同市| 房山区| 赣州市| 浙江省| 乐清市| 中超| 太白县| 栖霞市| 女性| 博湖县| 武胜县| 上栗县| 贵阳市| 永新县| 阿拉尔市| 巩留县| 武城县| 台湾省| 如东县| 桐乡市| 高唐县| 汉沽区| 开封市| 昭平县| 乌鲁木齐市| 中江县| 银川市| 思茅市| 虎林市| 封丘县| 建宁县| 虞城县| 陆河县| 鲁甸县| 西安市| 江津市| 龙南县| 乌审旗| 调兵山市| 金山区|