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

setTimeout

Timeouts can be used to defer the execution of a function until some number of milliseconds into the future.

Consider the following code:

setTimeout(a, 1000);
setTimeout(b, 1001);

One would expect that function b would execute after function a. However, this cannot be guaranteed — a may follow b, or the other way around.

Now, consider the subtle difference present in the following code snippet:

setTimeout(a, 1000);
setTimeout(b, 1000);

The execution order of a and b are predictable in this case. Node essentially maintains an object map grouping callbacks with identical timeout lengths. Isaac Schlueter, a former leader of the Node project and now CEO of npm Inc., puts it in this way:

As we can find on 

The ordering of timer callbacks registered within an identical execution scope does not predictably determine the eventual execution order in all cases. Additionally, there exists a minimum wait time of one millisecond for a timeout. Passing a value of zero, -1, or a non-number will be translated into this minimum value.

To cancel a timeout, use clearTimeout(timerReference).

主站蜘蛛池模板: 张家口市| 揭阳市| 庆元县| 察雅县| 天峨县| 西乌珠穆沁旗| 肥西县| 青铜峡市| 儋州市| 循化| 阿克苏市| 大田县| 体育| 营口市| 溆浦县| 昌邑市| 潜江市| 屏南县| 海安县| 米林县| 锡林浩特市| 四会市| 济南市| 灌阳县| 祁门县| 克东县| 蕲春县| 任丘市| 铜山县| 贵阳市| 扎鲁特旗| 会宁县| 梨树县| 湘乡市| 洱源县| 永胜县| 黄梅县| 桑日县| 峨眉山市| 天等县| 南昌县|