{"id":235209,"date":"2024-05-12T10:15:55","date_gmt":"2024-05-12T02:15:55","guid":{"rendered":""},"modified":"2024-05-12T10:16:03","modified_gmt":"2024-05-12T02:16:03","slug":"javascript%e5%a6%82%e4%bd%95%e5%81%9c%e6%ad%a2%e8%ae%a1%e6%97%b6%e5%99%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/235209.html","title":{"rendered":"Javascript\u5982\u4f55\u505c\u6b62\u8ba1\u65f6\u5668"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26234050\/3c8070ab-d80c-4fcc-ab90-1d22b8cb4692.webp\" alt=\"Javascript\u5982\u4f55\u505c\u6b62\u8ba1\u65f6\u5668\" \/><\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u505c\u6b62\u8ba1\u65f6\u5668\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4e24\u79cd\uff1a<strong><code>clearTimeout()<\/code> \u548c <code>clearInterval()<\/code><\/strong>\u3002<code>clearTimeout()<\/code> \u901a\u5e38\u7528\u4e8e\u505c\u6b62\u7531 <code>setTimeout()<\/code> \u65b9\u6cd5\u8bbe\u7f6e\u7684\u5ef6\u8fdf\u6267\u884c\u7684\u4ee3\u7801\u3002\u800c <code>clearInterval()<\/code> \u7528\u4e8e\u505c\u6b62\u7531 <code>setInterval()<\/code> \u65b9\u6cd5\u8bbe\u7f6e\u7684\u91cd\u590d\u6267\u884c\u7684\u4ee3\u7801\u3002<strong><code>clearTimeout()<\/code> \u65b9\u6cd5<\/strong> \u662f\u5176\u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u70b9\uff0c\u53ef\u7528\u6765\u963b\u6b62\u8ba1\u65f6\u5668\u6267\u884c\u5176\u5269\u4f59\u7684\u52a8\u4f5c\u3002\u5047\u8bbe\u4f60\u5df2\u7ecf\u4f7f\u7528 <code>setTimeout()<\/code> \u8bbe\u7f6e\u4e86\u4e00\u4e2a\u8ba1\u65f6\u5668\uff0c\u7136\u540e\u5728\u6307\u5b9a\u65f6\u95f4\u4e4b\u524d\uff0c\u4f60\u51b3\u5b9a\u4e0d\u518d\u9700\u8981\u5ef6\u8fdf\u6267\u884c\u7684\u4ee3\u7801\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c<code>clearTimeout()<\/code> \u5c31\u975e\u5e38\u6709\u7528\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u5373 <code>setTimeout()<\/code> \u8fd4\u56de\u7684\u6807\u8bc6\u7b26\uff0c\u7136\u540e\u53d6\u6d88\u8be5\u6807\u8bc6\u7b26\u6240\u4ee3\u8868\u7684\u8ba1\u65f6\u5668\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>clearTimeout()<\/code> \u505c\u6b62\u8ba1\u65f6\u5668<\/h3>\n<\/p>\n<p><p>\u5f53\u4f60\u901a\u8fc7 <code>setTimeout()<\/code> \u65b9\u6cd5\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u9700\u8981\u5728\u6307\u5b9a\u65f6\u95f4\u540e\u6267\u884c\u7684\u51fd\u6570\u65f6\uff0c\u5982\u679c\u51fa\u4e8e\u67d0\u79cd\u539f\u56e0\u4f60\u60f3\u53d6\u6d88\u8fd9\u4e2a\u8ba1\u65f6\u5668\uff0c<code>clearTimeout()<\/code> \u65b9\u6cd5\u5c31\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u9996\u5148\uff0c<code>setTimeout()<\/code> \u8fd4\u56de\u4e00\u4e2a\u6570\u5b57ID\uff0c\u8be5ID\u4ee3\u8868\u8bbe\u5b9a\u7684\u8ba1\u65f6\u5668\uff0c\u7136\u540e\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2aID\u4f5c\u4e3a <code>clearTimeout()<\/code> \u65b9\u6cd5\u7684\u53c2\u6570\u6765\u53d6\u6d88\u8fd9\u4e2a\u5ef6\u65f6\u6267\u884c\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u5b9e\u4f8b\u5e94\u7528\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">let timerId = setTimeout(() =&gt; alert(&quot;\u8fd9\u6bb5\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c&quot;), 1000);<\/p>\n<p>clearTimeout(timerId);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>setTimeout()<\/code> \u88ab\u7528\u6765\u8bbe\u5b9a\u4e00\u4e2a\u5ef6\u65f6\u64cd\u4f5c\uff0c\u4f46\u7d27\u63a5\u7740\u6211\u4eec\u8c03\u7528\u4e86 <code>clearTimeout()<\/code> \u5e76\u4f20\u5165\u4e86 <code>setTimeout()<\/code> \u8fd4\u56de\u7684ID\uff0c\u56e0\u6b64\u539f\u5b9a\u6267\u884c\u7684\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>clearInterval()<\/code> \u505c\u6b62\u8ba1\u65f6\u5668<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u90a3\u4e9b\u901a\u8fc7 <code>setInterval()<\/code> \u8bbe\u7f6e\u7684\u5468\u671f\u6027\u6267\u884c\u7684\u8ba1\u65f6\u5668\uff0c\u505c\u6b62\u5b83\u4eec\u7684\u5de5\u4f5c\u65b9\u5f0f\u7a0d\u6709\u4e0d\u540c\u3002<code>setInterval()<\/code> \u7528\u4e8e\u8bbe\u7f6e\u4e00\u4e2a\u51fd\u6570\u6bcf\u9694\u6307\u5b9a\u65f6\u95f4\u5c31\u6267\u884c\u4e00\u6b21\uff0c\u4f46\u5982\u679c\u4f60\u4e0d\u518d\u9700\u8981\u8fd9\u4e2a\u5468\u671f\u6027\u6267\u884c\u7684\u52a8\u4f5c\uff0c<code>clearInterval()<\/code> \u5c31\u80fd\u5e2e\u52a9\u4f60\u53d6\u6d88\u3002\u7c7b\u4f3c\u4e8e <code>clearTimeout()<\/code>\uff0c<code>clearInterval()<\/code> \u4e5f\u9700\u8981\u4e00\u4e2a\u7531 <code>setInterval()<\/code> \u8fd4\u56de\u7684\u6807\u8bc6\u7b26\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u5b9e\u4f8b\u5e94\u7528\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">let intervalId = setInterval(() =&gt; alert(&quot;\u8fd9\u6bb5\u4ee3\u7801\u5c06\u4e0d\u4f1a\u518d\u5468\u671f\u6027\u6267\u884c&quot;), 1000);<\/p>\n<p>clearInterval(intervalId);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>setInterval()<\/code> \u8bbe\u5b9a\u4e86\u4e00\u4e2a\u6bcf\u9694\u4e00\u79d2\u5c31\u4f1a\u6267\u884c\u7684\u51fd\u6570\u3002\u4f46\u901a\u8fc7\u4f7f\u7528 <code>clearInterval()<\/code> \u5e76\u4f20\u5165\u76f8\u5e94\u7684ID\uff0c\u6211\u4eec\u6210\u529f\u505c\u6b62\u4e86\u8fd9\u4e2a\u5468\u671f\u6027\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6b63\u786e\u7ba1\u7406\u8ba1\u65f6\u5668<\/h3>\n<\/p>\n<p><p>\u7ba1\u7406\u8ba1\u65f6\u5668\u4e0d\u4ec5\u4ec5\u662f\u80fd\u591f\u542f\u52a8\u548c\u505c\u6b62\u5b83\u4eec\u90a3\u4e48\u7b80\u5355\u3002\u8981\u6709\u6548\u4f7f\u7528\u8ba1\u65f6\u5668\uff0c\u4e86\u89e3\u4e24\u79cd\u65b9\u6cd5\u7684\u9002\u7528\u573a\u666f\u548c\u5982\u4f55\u907f\u514d\u5e38\u89c1\u9519\u8bef\u662f\u975e\u5e38\u5173\u952e\u7684\u3002\u4f8b\u5982\uff0c\u8bef\u5c06 <code>clearTimeout()<\/code> \u7528\u4e8e\u7531 <code>setInterval()<\/code> \u8bbe\u7f6e\u7684\u8ba1\u65f6\u5668\uff0c\u6216\u53cd\u4e4b\u3002<\/p>\n<\/p>\n<p><h4>\u907f\u514d\u5e38\u89c1\u9519\u8bef\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u6b63\u786e\u7684\u65b9\u6cd5\u5bf9\u5e94\u8ba1\u65f6\u5668\uff1a\u5bf9\u4e8e <code>setTimeout()<\/code> \u8bbe\u7f6e\u7684\u8ba1\u65f6\u5668\uff0c\u4f7f\u7528 <code>clearTimeout()<\/code> \u6765\u505c\u6b62\uff1b\u5bf9\u4e8e <code>setInterval()<\/code> \u8bbe\u7f6e\u7684\uff0c\u4f7f\u7528 <code>clearInterval()<\/code>\u3002<\/li>\n<li>\u4fdd\u5b58\u8ba1\u65f6\u5668ID\uff1a\u5728\u8c03\u7528 <code>setTimeout()<\/code> \u6216 <code>setInterval()<\/code> \u65f6\uff0c\u52a1\u5fc5\u4fdd\u5b58\u8fd4\u56de\u7684ID\uff0c\u8fd9\u6837\u624d\u80fd\u6b63\u786e\u5730\u505c\u6b62\u8ba1\u65f6\u5668\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f18\u5316\u8ba1\u65f6\u5668\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u4f18\u5316\u8ba1\u65f6\u5668\u7684\u4f7f\u7528\u53ef\u4ee5\u63d0\u9ad8\u4f60\u7684JavaScript\u5e94\u7528\u7684\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6709\u5927\u91cf\u4f7f\u7528 <code>setTimeout<\/code> \u6216 <code>setInterval<\/code> \u7684\u64cd\u4f5c\uff0c\u8003\u8651\u662f\u5426\u6bcf\u4e00\u4e2a\u90fd\u662f\u5fc5\u9700\u7684\uff0c\u4ee5\u53ca\u662f\u5426\u5b58\u5728\u66f4\u9ad8\u6548\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u6027\u80fd\u8003\u91cf\uff1a<\/h4>\n<\/p>\n<ul>\n<li>\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8ba1\u65f6\u5668\uff1a\u4ed4\u7ec6\u8bc4\u4f30\u4f60\u7684\u5e94\u7528\u662f\u5426\u771f\u7684\u9700\u8981\u6bcf\u4e00\u4e2a\u8ba1\u65f6\u5668\u3002\u6709\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u65b9\u5f0f\u66ff\u4ee3\u5b9a\u65f6\u5668\u7684\u9700\u6c42\u3002<\/li>\n<li>\u4f7f\u7528Web Workers\u5904\u7406\u590d\u6742\u4efb\u52a1\uff1a\u5982\u679c\u5b9a\u65f6\u5668\u7528\u4e8e\u5904\u7406\u590d\u6742\u8ba1\u7b97\uff0c\u8003\u8651\u4f7f\u7528Web Workers\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u540e\u53f0\u7ebf\u7a0b\u4e2d\u5904\u7406\u8ba1\u7b97\u4efb\u52a1\uff0c\u907f\u514d\u963b\u585e\u4e3b\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n<hr>\n<p><p><strong>\u603b\u7ed3<\/strong>\uff1a\u638c\u63e1\u5982\u4f55\u6b63\u786e\u505c\u6b62\u8ba1\u65f6\u5668\u5bf9\u4e8e\u5f00\u53d1\u9ad8\u6027\u80fdJavaScript\u5e94\u7528\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u65e0\u8bba\u662f <code>clearTimeout()<\/code> \u8fd8\u662f <code>clearInterval()<\/code>\uff0c\u6b63\u786e\u7684\u5e94\u7528\u5b83\u4eec\u53ef\u4ee5\u5e2e\u52a9\u4f60\u6709\u6548\u5730\u7ba1\u7406\u8ba1\u65f6\u5668\uff0c\u907f\u514d\u8d44\u6e90\u6d6a\u8d39\uff0c\u4fdd\u6301\u5e94\u7528\u7684\u54cd\u5e94\u6027\u548c\u7075\u6d3b\u6027\u3002\u540c\u65f6\uff0c\u4f18\u5316\u8ba1\u65f6\u5668\u7684\u4f7f\u7528\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\uff0c\u4e5f\u662f\u6bcf\u4e00\u4e2aJavaScript\u5f00\u53d1\u8005\u5e94\u8be5\u5173\u6ce8\u7684\u91cd\u70b9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728Javascript\u4e2d\u505c\u6b62\u8ba1\u65f6\u5668\uff1f<\/strong><br \/>\n\u8ba1\u65f6\u5668\u5728Javascript\u4e2d\u7531<code>setTimeout<\/code>\u548c<code>setInterval<\/code>\u51fd\u6570\u521b\u5efa\u3002\u8981\u505c\u6b62\u8ba1\u65f6\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528<code>clearTimeout<\/code>\u548c<code>clearInterval<\/code>\u51fd\u6570\u3002\u4f7f\u7528<code>clearTimeout<\/code>\u53ef\u4ee5\u505c\u6b62<code>setTimeout<\/code>\u521b\u5efa\u7684\u8ba1\u65f6\u5668\uff0c\u800c\u4f7f\u7528<code>clearInterval<\/code>\u53ef\u4ee5\u505c\u6b62<code>setInterval<\/code>\u521b\u5efa\u7684\u8ba1\u65f6\u5668\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u4f7f\u7528setTimeout\u521b\u5efa\u8ba1\u65f6\u5668\nvar timer = setTimeout(function() {\n  console.log(&quot;\u8ba1\u65f6\u5668\u5230\u671f\uff01&quot;);\n}, 5000);\n\n\/\/ \u505c\u6b62\u8ba1\u65f6\u5668\nclearTimeout(timer);\n\n\/\/ \u4f7f\u7528setInterval\u521b\u5efa\u8ba1\u65f6\u5668\nvar timer2 = setInterval(function() {\n  console.log(&quot;\u8ba1\u65f6\u5668\u89e6\u53d1\uff01&quot;);\n}, 1000);\n\n\/\/ \u505c\u6b62\u8ba1\u65f6\u5668\nclearInterval(timer2);\n<\/code><\/pre>\n<p><strong>2. \u5728Javascript\u4e2d\u5982\u4f55\u6682\u505c\u548c\u6062\u590d\u8ba1\u65f6\u5668\uff1f<\/strong><br \/>\n\u8981\u6682\u505c\u548c\u6062\u590d\u8ba1\u65f6\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6807\u5fd7\u53d8\u91cf\u6765\u63a7\u5236\u8ba1\u65f6\u5668\u7684\u6267\u884c\u548c\u6682\u505c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">var timer;\nvar paused = false;\n\n\/\/ \u5f00\u59cb\u8ba1\u65f6\u5668\nfunction startTimer() {\n  timer = setInterval(function() {\n    if (!paused) {\n      console.log(&quot;\u8ba1\u65f6\u5668\u89e6\u53d1\uff01&quot;);\n    }\n  }, 1000);\n}\n\n\/\/ \u6682\u505c\u8ba1\u65f6\u5668\nfunction pauseTimer() {\n  paused = true;\n}\n\n\/\/ \u6062\u590d\u8ba1\u65f6\u5668\nfunction resumeTimer() {\n  paused = false;\n}\n\n\/\/ \u505c\u6b62\u8ba1\u65f6\u5668\nfunction stopTimer() {\n  clearInterval(timer);\n}\n<\/code><\/pre>\n<p><strong>3. \u5982\u4f55\u5728Javascript\u4e2d\u5b9e\u73b0\u5b9a\u65f6\u505c\u6b62\u8ba1\u65f6\u5668\uff1f<\/strong><br \/>\n\u5982\u679c\u4f60\u5e0c\u671b\u8ba1\u65f6\u5668\u5728\u4e00\u5b9a\u65f6\u95f4\u540e\u81ea\u52a8\u505c\u6b62\uff0c\u53ef\u4ee5\u4f7f\u7528<code>setTimeout<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u5728<code>setTimeout<\/code>\u7684\u56de\u8c03\u51fd\u6570\u4e2d\uff0c\u8c03\u7528<code>clearInterval<\/code>\u6765\u505c\u6b62\u8ba1\u65f6\u5668\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u521b\u5efa\u8ba1\u65f6\u5668\u5e76\u572810\u79d2\u540e\u81ea\u52a8\u505c\u6b62\nvar timer = setInterval(function() {\n  console.log(&quot;\u8ba1\u65f6\u5668\u89e6\u53d1\uff01&quot;);\n}, 1000);\n\nsetTimeout(function() {\n  clearInterval(timer);\n  console.log(&quot;\u8ba1\u65f6\u5668\u505c\u6b62\uff01&quot;);\n}, 10000);\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u4e2d\uff0c\u505c\u6b62\u8ba1\u65f6\u5668\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4e24\u79cd\uff1aclearTimeout() \u548c clearInterv [&hellip;]","protected":false},"author":3,"featured_media":235219,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/235209"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=235209"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/235209\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/235219"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=235209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=235209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=235209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}