{"id":10209,"date":"2023-03-24T10:51:35","date_gmt":"2023-03-24T02:51:35","guid":{"rendered":"http:\/\/123.57.164.21\/?p=10209"},"modified":"2023-03-24T13:30:04","modified_gmt":"2023-03-24T05:30:04","slug":"javascript%e4%b8%ad%e5%ae%9e%e7%8e%b0sleep%e7%9a%84%e5%87%a0%e7%a7%8d%e7%ae%80%e5%8d%95%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=10209","title":{"rendered":"JavaScript\u4e2d\u5b9e\u73b0sleep\u7684\u51e0\u79cd\u7b80\u5355\u65b9\u6cd5"},"content":{"rendered":"\n<p><strong>\u4e00.\u4ec0\u4e48\u662fsleep\u51fd\u6570\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>sleep\u662f\u4e00\u79cd\u51fd\u6570\uff0c\u4ed6\u7684\u4f5c\u7528\u662f\u4f7f\u7a0b\u5e8f\u6682\u505c\u6307\u5b9a\u7684\u65f6\u95f4\uff0c\u8d77\u5230\u5ef6\u65f6\u7684\u6548\u679c\u3002<\/p>\n\n\n\n<p>\u5b98\u65b9\u4ecb\u7ecd\uff1asleep\u662f\u4e00\u79cd\u51fd\u6570\uff0c\u4f5c\u7528\u662f\u5ef6\u65f6\uff0c\u7a0b\u5e8f\u6682\u505c\u82e5\u5e72\u65f6\u95f4\uff0c\u5728\u6267\u884c\u65f6\u8981\u629b\u51fa\u4e00\u4e2a\u4e2d\u65ad\u5f02\u5e38\uff0c\u5fc5\u987b\u5bf9\u5176\u8fdb\u884c\u6355\u83b7\u5e76\u5904\u7406\u624d\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">console.log('1');\nsleep(2000);\nconsole.log('2');<\/pre>\n\n\n\n<p>\u63a7\u5236\u53f0\u8f93\u51fa\u6570\u5b571\u540e \u4f1a\u95f4\u96942\u79d2\u540e\u8f93\u51fa\u6570\u5b572<\/p>\n\n\n\n<p>\u5f53\u7136\u4e0a\u9762\u7684\u4ee3\u7801\u662f\u4e0d\u80fd\u6267\u884c\u7684\uff0c\u56e0\u4e3ajs\u4e2d\u662f\u6ca1\u6709sleep\u65b9\u6cd5\u7684\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u8fd9\u4e00\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u51e0\u79cd\u5728js\u4e2d\u5b9e\u73b0sleep\u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p><strong>\u4e8c.\u4e3a\u4ec0\u4e48\u4f7f\u7528sleep\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u770b\u5230\u8fd9\u91cc\u6709\u4eba\u4f1a\u95ee\u4e86\uff0c\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528sleep\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u6211\u53ef\u4ee5\u4f7f\u7528setTimeout\u6765\u5b9e\u73b0\u554a\uff1f<\/p>\n\n\n\n<p>\u56e0\u4e3asetTimeout\u662f\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u6765\u5b9e\u73b0<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1&amp;spm=1001.2101.3001.7020\" target=\"_blank\">\u5b9a\u65f6\u4efb\u52a1<\/a>\u7684\uff0c\u6240\u4ee5\u5728\u591a\u4efb\u52a1\u7684\u573a\u666f\u4e0b\u5c31\u4f1a\u51fa\u73b0\u56de\u8c03\u5d4c\u5957\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">console.time('runTime:');\n  setTimeout(() => {\n    console.log('1');\n    setTimeout(() => {\n      console.log('2')\n      setTimeout(() => {\n        console.log('3')\n        console.timeEnd('runTime:');\n      }, 2000);\n    }, 3000);\n  }, 2000);\n\/\/\u7ed3\u679c\uff1a\n\/\/1\n\/\/2\n\/\/3\n\/\/runTime:: 7017.87890625 ms<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u65b9\u5f0f\u5b58\u5728\u56de\u8c03\u5d4c\u5957\u7684\u95ee\u9898\uff0c\u6211\u4eec\u5e0c\u671b\u53ef\u4ee5\u5229\u7528sleep\u51fd\u6570\u66f4\u65b9\u4fbf\u4f18\u96c5\u5730\u5b9e\u73b0\u4e0a\u9762\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<p><strong>\u4e09.\u5b9e\u73b0sleep<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u5206\u522b\u7528\u51e0\u79cd\u4e0d\u540c\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u4e0bsleep\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p>1.<strong>\u57fa\u4e8eDate\u5b9e\u73b0<\/strong><\/p>\n\n\n\n<p>\u901a\u8fc7\u6b7b\u5faa\u73af\u6765\u963b\u6b62\u4ee3\u7801\u6267\u884c\uff0c\u540c\u65f6\u4e0d\u505c\u6bd4\u5bf9\u662f\u5426\u8d85\u65f6\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function sleep(time){\n var timeStamp = new Date().getTime();\n var endTime = timeStamp + time;\n while(true){\n if (new Date().getTime() > endTime){\n  return;\n } \n }\n}\nconsole.time('runTime:');\nsleep(2000);\nconsole.log('1');\nsleep(3000);\nconsole.log('2');\nsleep(2000);\nconsole.log('3');\nconsole.timeEnd('runTime:');\n\/\/ 1\n\/\/ 2\n\/\/ 3\n\/\/ runTime:: 7004.301ms<\/pre>\n\n\n\n<p>\u7f3a\u70b9\uff1a<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u7684\u4ee3\u7801\u4e0d\u4f1a\u8ba9\u7ebf\u7a0b\u4f11\u7720\uff0c\u800c\u662f\u901a\u8fc7\u9ad8\u8d1f\u8377\u8ba1\u7b97\u4f7fcpu\u65e0\u6687\u5904\u7406\u5176\u4ed6\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\u505a\u7684\u7f3a\u70b9\u662f\u5728sleep\u7684\u8fc7\u7a0b\u4e2d\u5176\u4ed6\u6240\u6709\u7684\u4efb\u52a1\u90fd\u4f1a\u88ab\u6682\u505c\uff0c\u5305\u62ecdom\u7684\u6e32\u67d3\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5sleep\u7684\u8fc7\u7a0b\u4e2d\u7a0b\u5e8f\u4f1a\u5904\u4e8e\u5047\u6b7b\u72b6\u6001\uff0c\u5e76\u4e0d\u4f1a\u53bb\u6267\u884c\u5176\u4ed6\u4efb\u52a1<\/p>\n\n\n\n<p><strong>2.\u57fa\u4e8ePromise\u7684sleep<\/strong><\/p>\n\n\n\n<p>\u5355\u7eaf\u7684Promise\u53ea\u662f\u5c06\u4e4b\u524d\u7684\u7eb5\u5411\u5d4c\u5957\u6539\u4e3a\u4e86\u6a2a\u5411\u5d4c\u5957\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function sleep(time){\n return new Promise(function(resolve){\n setTimeout(resolve, time);\n });\n}\nconsole.time('runTime:');\nconsole.log('1');\nsleep(1000).then(function(){\n console.log('2');\n sleep(2000).then(function(){\n console.log('3');\n console.timeEnd('runTime:');\n });\n});\nconsole.log('a');\n\/\/ 1\n\/\/ a\n\/\/ 2\n\/\/ 3\n\/\/ runTime:: 3013.476ms<\/pre>\n\n\n\n<p>\u8fd9\u5176\u5b9e\u548c\u4e4b\u524d\u7684setTimeout\u5d4c\u5957\u6ca1\u4ec0\u4e48\u533a\u522b\uff0c\u4e5f\u5f88\u96be\u770b\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u518d\u6b21\u8fdb\u884c\u4f18\u5316\uff0c\u4f7f\u7528ES6\u7684<a rel=\"noreferrer noopener\" href=\"https:\/\/so.csdn.net\/so\/search?q=Generator%E5%87%BD%E6%95%B0&amp;spm=1001.2101.3001.7020\" target=\"_blank\">Generator\u51fd\u6570<\/a>\u6765\u6539\u5199\u4e0a\u9762\u7684\u4f8b\u5b50<\/p>\n\n\n\n<p>3.<strong>\u57fa\u4e8eGenerator\u51fd\u6570\u7684sleep<\/strong><\/p>\n\n\n\n<p>\u6211\u4eec\u5bf9sleep\u7684\u6267\u884c\u4f7f\u7528Generator\u51fd\u6570\u6765\u6267\u884c\uff0c\u5e76\u4e14\u642d\u914dco\u6765\u8fdb\u884c\u81ea\u6267\u884c\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">var co = require('co');\n \nfunction sleep(time){\n return new Promise(function(resolve){\n setTimeout(resolve, time);\n });\n}\n \nvar run = function* (){\n console.time('runTime:');\n console.log('1');\n yield sleep(2000);\n console.log('2');\n yield sleep(1000);\n console.log('3'); \n console.timeEnd('runTime:');\n}\n \nco(run);\nconsole.log('a');\n\/\/ 1\n\/\/ a\n\/\/ 2\n\/\/ 3\n\/\/ runTime:: 3004.935ms<\/pre>\n\n\n\n<p>\u4ee5\u770b\u5230\u6574\u4f53\u7684\u4ee3\u7801\u770b\u8d77\u6765\u4e0d\u5b58\u5728\u5d4c\u5957\u7684\u5173\u7cfb\uff0c\u5e76\u4e14\u6267\u884c\u8fc7\u7a0b\u4e0d\u4f1a\u53d1\u751f\u5047\u6b7b\u60c5\u51b5\uff0c\u4e0d\u4f1a\u963b\u585e\u5176\u4ed6\u4efb\u52a1\u7684\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u591a\u4e86\u4e00\u4e2aco\u6267\u884c\u5668\u7684\u5f15\u7528\uff0c\u6240\u4ee5\u8fd8\u662f\u6709\u7455\u75b5\u3002<\/p>\n\n\n\n<p>4.<strong>\u57fa\u4e8easync\u51fd\u6570\u7684sleep<\/strong><\/p>\n\n\n\n<p>async\u51fd\u6570\u6700\u5927\u7684\u7279\u70b9\u5c31\u662f\u81ea\u5e26\u6267\u884c\u5668\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4e0d\u501f\u52a9co\u6765\u5b9e\u73b0sleep\u4e86<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function sleep(time){\n return new Promise((resolve) => setTimeout(resolve, time));\n}\n \nasync function run(){\n console.time('runTime:');\n console.log('1');\n await sleep(2000);\n console.log('2');\n await sleep(1000);\n console.log('3'); \n console.timeEnd('runTime:');\n}\n \nrun();\nconsole.log('a');\n \n\/\/ 1\n\/\/ a\n\/\/ 2\n\/\/ 3\n\/\/ runTime:: 3009.984ms<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00.\u4ec0\u4e48\u662fsleep\u51fd\u6570\uff1f sleep\u662f\u4e00\u79cd\u51fd\u6570\uff0c\u4ed6\u7684\u4f5c\u7528\u662f\u4f7f\u7a0b\u5e8f\u6682\u505c\u6307\u5b9a\u7684\u65f6\u95f4\uff0c\u8d77\u5230\u5ef6\u65f6\u7684\u6548\u679c\u3002 \u5b98\u65b9\u4ecb\u7ecd\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10209"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10209"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10209\/revisions"}],"predecessor-version":[{"id":10212,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10209\/revisions\/10212"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}