{"id":2515,"date":"2018-02-23T13:57:14","date_gmt":"2018-02-23T05:57:14","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2515"},"modified":"2018-02-23T13:57:14","modified_gmt":"2018-02-23T05:57:14","slug":"settimeout-%e5%92%8c-setimmediate","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2515","title":{"rendered":"setTimeout \u548c setImmediate"},"content":{"rendered":"<p>\n\t\u7531\u4e8esetTimeout\u5728 timers \u9636\u6bb5\u6267\u884c\uff0c\u800csetImmediate\u5728 check \u9636\u6bb5\u6267\u884c\u3002\u6240\u4ee5\uff0csetTimeout\u4f1a\u65e9\u4e8esetImmediate\u5b8c\u6210\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\nsetTimeout(() =&gt; console.log(1));\r\nsetImmediate(() =&gt; console.log(2));<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u5e94\u8be5\u5148\u8f93\u51fa1\uff0c\u518d\u8f93\u51fa2\uff0c\u4f46\u662f\u5b9e\u9645\u6267\u884c\u7684\u65f6\u5019\uff0c\u7ed3\u679c\u5374\u662f\u4e0d\u786e\u5b9a\uff0c\u6709\u65f6\u8fd8\u4f1a\u5148\u8f93\u51fa2\uff0c\u518d\u8f93\u51fa1\u3002\n<\/p>\n<p>\n\t\u8fd9\u662f\u56e0\u4e3a<code>setTimeout<\/code>\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u9ed8\u8ba4\u4e3a<code>0<\/code>\u3002\u4f46\u662f\u5b9e\u9645\u4e0a\uff0cNode \u505a\u4e0d\u52300\u6beb\u79d2\uff0c\u6700\u5c11\u4e5f\u9700\u89811\u6beb\u79d2\uff0c\u6839\u636e<a href=\"https:\/\/nodejs.org\/api\/timers.html#timers_settimeout_callback_delay_args\" target=\"_blank\">\u5b98\u65b9\u6587\u6863<\/a>\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u53d6\u503c\u8303\u56f4\u57281\u6beb\u79d2\u52302147483647\u6beb\u79d2\u4e4b\u95f4\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c<code>setTimeout(f, 0)<\/code>\u7b49\u540c\u4e8e<code>setTimeout(f, 1)<\/code>\u3002\n<\/p>\n<p>\n\t\u5b9e\u9645\u6267\u884c\u7684\u65f6\u5019\uff0c\u8fdb\u5165\u4e8b\u4ef6\u5faa\u73af\u4ee5\u540e\uff0c\u6709\u53ef\u80fd\u5230\u4e861\u6beb\u79d2\uff0c\u4e5f\u53ef\u80fd\u8fd8\u6ca1\u52301\u6beb\u79d2\uff0c\u53d6\u51b3\u4e8e\u7cfb\u7edf\u5f53\u65f6\u7684\u72b6\u51b5\u3002\u5982\u679c\u6ca1\u52301\u6beb\u79d2\uff0c\u90a3\u4e48 timers \u9636\u6bb5\u5c31\u4f1a\u8df3\u8fc7\uff0c\u8fdb\u5165 check \u9636\u6bb5\uff0c\u5148\u6267\u884c<code>setImmediate<\/code>\u7684\u56de\u8c03\u51fd\u6570\u3002\n<\/p>\n<p>\n\t\u4f46\u662f\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u4e00\u5b9a\u662f\u5148\u8f93\u51fa2\uff0c\u518d\u8f93\u51fa1\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\nconst fs = require(&#039;fs&#039;);\r\n\/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\nfs.readFile(&#039;test.js&#039;, () =&gt; {\r\n  setTimeout(() =&gt; console.log(1));\r\n  setImmediate(() =&gt; console.log(2));\r\n});<\/pre>\n<p>\n\t\u4e0a\u9762\u4ee3\u7801\u4f1a\u5148\u8fdb\u5165 I\/O callbacks \u9636\u6bb5\uff0c\u7136\u540e\u662f check \u9636\u6bb5\uff0c\u6700\u540e\u624d\u662f timers \u9636\u6bb5\u3002\u56e0\u6b64\uff0csetImmediate\u624d\u4f1a\u65e9\u4e8esetTimeout\u6267\u884c\u3002\n<\/p>\n<h2>\n\t\u53c2\u8003\u94fe\u63a5<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t<a href=\"https:\/\/nodejs.org\/en\/docs\/guides\/event-loop-timers-and-nexttick\/\" target=\"_blank\">The Node.js Event Loop, Timers, and process.nextTick()<\/a>, by Node.js\n\t<\/li>\n<li>\n\t\t<a href=\"https:\/\/jsblog.insiderattack.net\/handling-io-nodejs-event-loop-part-4-418062f917d1\" target=\"_blank\">Handling IO\u200a&#8211;\u200aNodeJS Event Loop<\/a>, by Deepal Jayasekara\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/voidcanvas.com\/setimmediate-vs-nexttick-vs-settimeout\/\" target=\"_blank\">setImmediate() vs nextTick() vs setTimeout(fn,0) &#8211; in depth explanation<\/a>, by Paul Shan\n\t<\/li>\n<li>\n\t\t<a href=\"http:\/\/voidcanvas.com\/nodejs-event-loop\/\" target=\"_blank\">Node.js event loop workflow &amp; lifecycle in low level<\/a>, by Paul Shan\n\t<\/li>\n<\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2515\">setTimeout \u548c setImmediate<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u7531\u4e8esetTimeout\u5728 timers \u9636\u6bb5\u6267\u884c\uff0c\u800csetImmediate\u5728 check \u9636\u6bb5\u6267\u884c\u3002\u6240\u4ee5\uff0c [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[119,1106,1604],"class_list":["post-2515","post","type-post","status-publish","format-standard","hentry","category-html5","tag-html5","tag-javascript","tag-settimeout"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2515"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2515\/revisions"}],"predecessor-version":[{"id":2516,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2515\/revisions\/2516"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}