{"id":10006485,"date":"2022-09-15T12:42:45","date_gmt":"2022-09-15T04:42:45","guid":{"rendered":"https:\/\/ityet.com\/?p=10006485"},"modified":"2022-09-15T12:42:45","modified_gmt":"2022-09-15T04:42:45","slug":"%e5%9b%be%e8%a7%a3js%e4%b8%ad%e7%9a%84%e4%ba%8b%e4%bb%b6%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006485","title":{"rendered":"\u56fe\u89e3JS\u4e2d\u7684\u4e8b\u4ef6\u5faa\u73af"},"content":{"rendered":"<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2qqO\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<blockquote>\n<p>1\u3001js\u5355\u7ebf\u7a0b\uff0c\u5e76\u975e\u6307js\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u800c\u662f\u540c\u4e00\u65f6\u523b\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5728\u5de5\u4f5c\u3002<\/p>\n<p>2\u3001js\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u4e4b\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u7ebf\u7a0b\uff0c\u6bd4\u5982\u4e8b\u4ef6\u5faa\u73af\u7ebf\u7a0b\uff0c\u5b9a\u65f6\u5668\u89e6\u53d1\u7ebf\u7a0b\uff0chttp\u5f02\u6b65\u7ebf\u7a0b\uff0c \u6d4f\u89c8\u5668\u4e8b\u4ef6\u7ebf\u7a0b\u3002<\/p>\n<p>3\u3001\u5728js\u4e3b\u7ebf\u7a0b\u4e2d\uff0c\u5206\u4e3a\u4e24\u4e2a\u5b50\u7ebf\u7a0b\uff0cjs\u5f15\u64ce\u7ebf\u7a0b\uff0cGUI\u6e32\u67d3\u7ebf\u7a0b\u3002\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u662f\u4e92\u65a5\u7684\uff0c\u540c\u4e00\u65f6\u523b\u53ea\u80fd\u6267\u884c\u4e00\u4e2a\uff0c\u8981\u4e48\u6267\u884cjs\uff0c\u8981\u4e48\u6e32\u67d3html<\/p>\n<p>4\u3001\u4efb\u52a1\u961f\u5217\u4e2d\uff0c\u5206\u4e3a\u5b8f\u4efb\u52a1\u5fae\u4efb\u52a1\u3002\u6bcf\u6b21\u6267\u884c\u4efb\u52a1\u961f\u5217\u65f6\uff0c\u5148\u6267\u884c\u5fae\u4efb\u52a1\uff0c\u518d\u6267\u884c\u5b8f\u4efb\u52a1\u3002<\/p>\n<p>5\u3001\u901a\u5e38\u5b8f\u4efb\u52a1\u6307 setTimeout\uff0csetInterval\uff0cXMLHttprequest\uff0cfetch\u7b49\u56de\u8c03\u3002 \u5fae\u4efb\u52a1\u6307  Promise\uff0cMutationObserver\u7b49\u56de\u8c03\u3002<\/p>\n<p>6\u3001\u5982\u679c\u5728 \u5b9a\u65f6\u5668\u89e6\u53d1\u7ebf\u7a0b\u3001http\u5f02\u6b65\u7ebf\u7a0b\u3001\u6d4f\u89c8\u5668\u4e8b\u4ef6\u7ebf\u7a0b\u4e2d\uff0c\u6ca1\u6709\u56de\u8c03\uff0c\u5219\u4e0d\u4f1a\u653e\u5165\u961f\u5217\u4e2d\u3002<\/p>\n<p>7\u3001\u4e8b\u4ef6\u5faa\u73af\u7ebf\u7a0b\uff0c\u5fc5\u987b\u7b49\u5f85\u4e3b\u7ebf\u7a0b\u4e2d\u7684\u540c\u6b65\u4ee3\u7801\u6267\u884c\u7ed3\u675f\uff0c\u624d\u4f1a\u53bb\u4efb\u52a1\u961f\u5217\u518d\u53d6\u4e00\u4e2a \u4efb\u52a1\u653e\u5165 \u4e3b\u7ebf\u7a0b\u4e2d\u6267\u884c\u3002<\/p>\n<\/blockquote>\n<pre><code class=\"javascript\">\/\/index.js\nconsole.log('a');\n\n  Promise.resolve().then(() =&gt; {\n    console.log('b');\n  });\n\n  setTimeout(() =&gt; {\n    console.log('c');\n  }, 0);\n\n\n  setTimeout(() =&gt; {\n    console.log('d');\n\n    Promise.resolve().then(() =&gt; {\n      console.log('e');\n    });\n  }, 0);\n\n\n  console.log('f');<\/code><\/pre>\n<p>\u4e0b\u9762\u662f\u4e0a\u8ff0\u4ee3\u7801\u7684\u6267\u884c\u903b\u8f91\uff1a<\/p>\n<ol>\n<li>\u9047\u5230console.log(&#8216;a&#8217;); \u540c\u6b65\u4ee3\u7801\uff0c\u6267\u884c\uff0c\u8f93\u51faa\uff1b<\/li>\n<li>\u9047\u5230Promise\uff0c\u5f02\u6b65\u4ee3\u7801\uff0c\u653e\u5165\u4efb\u52a1\u961f\u5217\u3002\u53c8\u56e0\u4e3a\u662fpromise\u7684\u56de\u8c03\uff0c\u5c5e\u4e8e\u5fae\u4efb\u52a1\u3002\u6807\u8bb0\u5fae\u4efb\u52a11<\/li>\n<li>\u9047\u5230setTimeout\u6267\u884c\uff0c\u653e\u5165\u5b9a\u5236\u5668\u89e6\u53d1\u7ebf\u7a0b\u4e2d\uff0c\u5b9a\u65f6\u5668\u89e6\u53d1\u7ebf\u7a0b\u4e2d\u7ef4\u62a4\u4f55\u65f6\u5012\u8ba1\u65f6\u7ed3\u675f\uff0c\u5e76\u5c06\u56de\u8c03\u653e\u5165\u4efb\u52a1\u961f\u5217\u3002\u53c8\u56e0\u4e3asetTimeout\u7684\u56de\u8c03\u5c5e\u4e8e\u5b8f\u4efb\u52a1\u3002\u6807\u8bb0\u4e3a\u5b8f\u4efb\u52a11<\/li>\n<li>\u53c8\u9047\u5230setTimeout\u6267\u884c\uff0c\u653e\u5165\u5b9a\u5236\u5668\u89e6\u53d1\u7ebf\u7a0b\u4e2d\uff0c\u5c06\u56de\u8c03\u653e\u5165\u4efb\u52a1\u961f\u5217\u3002\u56e0\u4e3asetTimeout\u7684\u56de\u8c03\u5c5e\u4e8e\u5b8f\u4efb\u52a1\u3002\u6807\u8bb0\u4e3a\u5b8f\u4efb\u52a12<\/li>\n<li>\u9047\u5230console.log(&#8216;f&#8217;); \u540c\u6b65\u4ee3\u7801\uff0c\u6267\u884c\uff0c\u8f93\u51faf<\/li>\n<\/ol>\n<p>\u6b64\u65f6\u4e3b\u7ebf\u7a0b\u4e2d\u7684\u540c\u6b65\u4ee3\u7801\u5df2\u7ecf\u5b8c\u5168\u6267\u884c\uff0c\u63a7\u5236\u53f0\u8f93\u51faa\uff0cf\u3002\u4e3b\u7ebf\u7a0b\u662f\u7a7a\u7684\u3002\u6b64\u65f6\u4e8b\u4ef6\u5faa\u73af\u7ebf\u7a0b\u53d1\u73b0\uff0c\u4efb\u52a1\u961f\u5217\u6709\u4e1c\u897f\uff0c\u5206\u522b\u662f\u5fae\u4efb\u52a11\uff0c\u5b8f\u4efb\u52a11\uff0c\u5b8f\u4efb\u52a12.<\/p>\n<ol start=\"6\">\n<li>\u6309\u7167\u5148\u6267\u884c\u5fae\u4efb\u52a1\uff0c\u518d\u6267\u884c\u5b8f\u4efb\u52a1\u987a\u5e8f\uff0c\u5148\u5c06\u5fae\u4efb\u52a11\uff0c\u5373  () =&gt; { console.log(&#8216;b&#8217;); } \u653e\u5165\u4e3b\u7ebf\u7a0b\u4e2d\u7531js\u6267\u884c\u3002\u8f93\u51fab\uff0c<\/li>\n<li>\u6b64\u65f6\u4e3b\u7ebf\u7a0b\u6267\u884c\u5b8c\uff0c\u53c8\u7a7a\u4e86\uff0c\u6b64\u65f6\u4efb\u52a1\u961f\u5217\u8fd8\u6709\u5b8f\u4efb\u52a11\uff0c\u5b8f\u4efb\u52a12\u3002\u7531\u4e8e\u5b8f\u4efb\u52a11\u5148\u653e\u5165\u7684\uff0c\u6309\u7167\u961f\u5217\u7684\u5148\u8fdb\u5148\u51fa\u987a\u5e8f\u3002\u5148\u5c06\u5b8f\u4efb\u52a11\u653e\u5165\u4e3b\u7ebf\u7a0b\u3002\u5373 () =&gt; { console.log(&#8216;c&#8217;); }\uff0c\u8f93\u51fac\uff0c<\/li>\n<li>\u518d\u5224\u65ad\u961f\u5217\u4e2d\u662f\u5426\u6709\u5fae\u4efb\u52a1\uff0c\u5982\u679c\u6709\uff0c\u5219\u5168\u90e8\u6267\u884c\u3002\u5982\u679c\u6ca1\u6709\uff0c\u5c31\u7ee7\u7eed\u6267\u884c\u5b8f\u4efb\u52a12.<\/li>\n<li>\n<p>\u5c06\u5b8f\u4efb\u52a12\u653e\u5165\u4e3b\u7ebf\u7a0b\uff0c\u5373<\/p>\n<pre><code>() =&gt; {\n console.log('d');\n\n Promise.resolve().then(() =&gt; {\n   console.log('e');\n });\n}<\/code><\/pre>\n<p>\u8f93\u51fad\uff0c\u9047\u5230promise\uff0c\u5f02\u6b65\u4ee3\u7801\uff0c\u653e\u5165\u5fae\u4efb\u52a1\u961f\u5217\u3002\u6807\u8bb0\u4e3a\u5fae\u4efb\u52a12\u3002\u6b64\u65f6\u4e3b\u7ebf\u7a0b\u53c8\u7a7a\u4e86\u3002<\/p>\n<\/li>\n<li>\u6b64\u65f6\u4efb\u52a1\u961f\u5217\u53ea\u6709\u5fae\u4efb\u52a12\uff0c\u6ca1\u6709\u5176\u4ed6\u7684\u5b8f\u4efb\u52a1\u548c\u5fae\u4efb\u52a1\u3002 \u6700\u540e\u518d\u6267\u884c\u5fae\u4efb\u52a12\u3002\u5373 () =&gt; { console.log(&#8216;e&#8217;); }\uff0c \u8f93\u51fae<\/li>\n<\/ol>\n<p>\u603b\u7ed3\u4e0b: \u6700\u540e\u8f93\u51fa\u7ed3\u679c\u4e3a a  f  b  c  d  e<\/p>\n<p><strong>\u6ce8\u610f: <\/strong><br \/>1\u3001\u4e0a\u9762\u7684  setTimeout(()=&gt;{}); \u5c5e\u4e8e\u540c\u6b65\u4ee3\u7801\uff0c\u4f1a\u6267\u884c\uff0c\u5982\u679c let timer = setTimeout(()=&gt;{}); \u4f60\u4f1a\u53d1\u73b0timer\u6709\u503c\uff0c\u662f\u4e2a\u6570\u5b57\u3002\u4f46\u4e5f\u4ec5\u4ec5\u662f\u6267\u884c setTimeout\u540e\u5c06\u5f15\u7528\u8fd4\u56de\uff0c\u5269\u4e0b\u7684\u5012\u8ba1\u65f6\u548c\u56de\u8c03\u3002\u90fd\u5728\u5b9a\u65f6\u5668\u89e6\u53d1\u7ebf\u7a0b\u4e2d\u7ef4\u62a4\u3002<br \/>2\u3001\u540c\u6837\uff0c\u4e0a\u9762\u7684 Promise.resolve() \u4e5f\u5c5e\u4e8e\u540c\u6b65\u4ee3\u7801\uff0clet p = Promise.resolve() .\u4f1a\u53d1\u73b0p\u6709\u503c\uff0c\u662f\u4e2aPromise\u5bf9\u8c61\uff0c\u4f46\u4e5f\u4ec5\u4ec5\u662f\u6267\u884c  Promise.resolve() \u540e\u5c06\u5f15\u7528\u8fd4\u56de\uff0c\u5269\u4e0b\u7684then\u4e2d\u7684\u56de\u8c03\u3002\u90fd\u5728\u5fae\u4efb\u52a1\u961f\u5217\u4e2d\u7ef4\u62a4<\/p>\n<p>\u4ee5\u4e0a\u662f\u81ea\u5df1\u901a\u8fc7\u9605\u8bfb\u5176\u4ed6\u6587\u7ae0\uff0c\u52a0\u4e0a\u81ea\u5df1\u7406\u89e3\u548c\u672c\u5730\u8c03\u8bd5\u4e4b\u540e\u7684 \u4e00\u70b9\u70b9\u611f\u60f3\u3002 \u5982\u6709\u95ee\u9898\uff0c\u8fd8\u671b\u6307\u6b63\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042485178\">https:\/\/segmentfault.com\/a\/1190000042485178<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001js\u5355\u7ebf\u7a0b\uff0c\u5e76\u975e\u6307js\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u800c\u662f\u540c\u4e00\u65f6\u523b\u53ea\u80fd\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5728\u5de5\u4f5c\u3002 2\u3001js\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u4e4b\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u7ebf [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006485","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006485"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006485\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}