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

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).

主站蜘蛛池模板: 黑河市| 汝南县| 宜昌市| 清丰县| 嵩明县| 稷山县| 神农架林区| 新乐市| 常山县| 长子县| 徐闻县| 磴口县| 雅江县| 黔西县| 称多县| 故城县| 鄄城县| 涿鹿县| 陇南市| 施甸县| 登封市| 大名县| 乐安县| 沙坪坝区| 石河子市| 晋江市| 防城港市| 南岸区| 凤凰县| 镇巴县| 新野县| 望都县| 贵州省| 高要市| 石门县| 榕江县| 厦门市| 台江县| 利津县| 临武县| 南和县|