{"id":297444,"date":"2024-05-20T16:27:54","date_gmt":"2024-05-20T08:27:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/297444.html"},"modified":"2024-05-20T16:28:15","modified_gmt":"2024-05-20T08:28:15","slug":"%e5%89%8d%e7%ab%af-js-%e5%a6%82%e4%bd%95%e9%98%b2%e6%ad%a2%e9%87%8d%e5%a4%8d%e6%8f%90%e4%ba%a4%e8%a1%a8%e5%8d%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/297444.html","title":{"rendered":"\u524d\u7aef js \u5982\u4f55\u9632\u6b62\u91cd\u590d\u63d0\u4ea4\u8868\u5355"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27145201\/9c7434e3-0ae8-423d-aec1-36d572b87fb2.webp\" alt=\"\u524d\u7aef js \u5982\u4f55\u9632\u6b62\u91cd\u590d\u63d0\u4ea4\u8868\u5355\" \/><\/p>\n<p><p>JavaScript\u5728\u524d\u7aef\u5f00\u53d1\u4e2d\u626e\u6f14\u7740\u81f3\u5173\u91cd\u8981\u7684\u89d2\u8272\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u8868\u5355\u63d0\u4ea4\u65f6\u3002\u4e3a\u4e86\u9632\u6b62\u91cd\u590d\u63d0\u4ea4\u8868\u5355\uff0c\u53ef\u4ee5\u91c7\u53d6\u51e0\u79cd\u7b56\u7565\uff0c\u5b83\u4eec\u5305\u62ec<strong>\u7981\u7528\u63d0\u4ea4\u6309\u94ae\u3001\u8bbe\u7f6e\u6807\u5fd7\u53d8\u91cf\u3001\u4f7f\u7528\u65f6\u95f4\u6233\u6216\u8ba1\u6570\u5668\u3001\u670d\u52a1\u5668\u7aef\u9a8c\u8bc1<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u7981\u7528\u63d0\u4ea4\u6309\u94ae<\/strong>\u662f\u6700\u76f4\u89c2\u4e14\u5b9e\u65bd\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u5728\u7528\u6237\u63d0\u4ea4\u8868\u5355\u540e\uff0c\u7acb\u5373\u901a\u8fc7JavaScript\u7981\u7528\u63d0\u4ea4\u6309\u94ae\uff0c\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u56e0\u4e3a\u7f51\u7edc\u5ef6\u8fdf\u6216\u662f\u7528\u6237\u591a\u6b21\u70b9\u51fb\u9020\u6210\u7684\u91cd\u590d\u63d0\u4ea4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7981\u7528\u63d0\u4ea4\u6309\u94ae<\/h3>\n<\/p>\n<p><p>\u5f53\u7528\u6237\u70b9\u51fb\u63d0\u4ea4\u6309\u94ae\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7JavaScript\u4e3a\u8be5\u6309\u94ae\u6dfb\u52a0\u4e00\u4e2a\u5c5e\u6027\u6765\u4f7f\u5176\u53d8\u4e3a\u4e0d\u53ef\u70b9\u51fb\u72b6\u6001\u3002\u8fd9\u6837\u505a\u4e0d\u4ec5\u53ef\u4ee5\u9632\u6b62\u7528\u6237\u56e0\u4e3a impatient \u6216\u7f51\u7edc\u5ef6\u8fdf\u800c\u591a\u6b21\u70b9\u51fb\u63d0\u4ea4\u6309\u94ae\uff0c\u540c\u65f6\u4e5f\u5411\u7528\u6237\u53cd\u9988\u4e86\u4e00\u4e2a\u201c\u5df2\u54cd\u5e94\u201d\u7684\u4fe1\u53f7\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9e\u73b0\u65b9\u6cd5<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u83b7\u53d6\u63d0\u4ea4\u6309\u94ae<\/p>\n<p>const submitButton = document.querySelector(&#039;#submit-button&#039;);<\/p>\n<p>\/\/ \u7ed1\u5b9a\u70b9\u51fb\u4e8b\u4ef6<\/p>\n<p>submitButton.addEventListener(&#039;click&#039;, function() {<\/p>\n<p>  \/\/ \u7981\u7528\u6309\u94ae<\/p>\n<p>  this.disabled = true;<\/p>\n<p>  \/\/ \u63d0\u4ea4\u8868\u5355<\/p>\n<p>  \/\/ \u7701\u7565\u5177\u4f53\u63d0\u4ea4\u903b\u8f91<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u56e0\u7528\u6237\u591a\u6b21\u70b9\u51fb\u800c\u5bfc\u81f4\u7684\u91cd\u590d\u63d0\u4ea4\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6e\u6807\u5fd7\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5728JavaScript\u4e2d\u8bbe\u7f6e\u4e00\u4e2a\u6807\u5fd7\u53d8\u91cf\uff0c\u7528\u4ee5\u6807\u8bc6\u8868\u5355\u662f\u5426\u6b63\u5728\u88ab\u63d0\u4ea4\u3002\u5982\u679c\u8868\u5355\u6b63\u5728\u63d0\u4ea4\u4e2d\uff0c\u540e\u7eed\u7684\u63d0\u4ea4\u8bf7\u6c42\u5c06\u4f1a\u88ab\u5ffd\u7565\u76f4\u5230\u5f53\u524d\u63d0\u4ea4\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9e\u65bd\u65b9\u5f0f<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let isSubmitting = false;<\/p>\n<p>\/\/ \u63d0\u4ea4\u8868\u5355\u7684\u51fd\u6570<\/p>\n<p>function submitForm() {<\/p>\n<p>  if (isSubmitting) {<\/p>\n<p>    \/\/ \u5982\u679c\u6b63\u5728\u63d0\u4ea4\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\uff0c\u4e0d\u505a\u4efb\u4f55\u5904\u7406<\/p>\n<p>    return;<\/p>\n<p>  }<\/p>\n<p>  \/\/ \u6807\u8bb0\u4e3a\u6b63\u5728\u63d0\u4ea4<\/p>\n<p>  isSubmitting = true;<\/p>\n<p>  \/\/ \u6a21\u62df\u5f02\u6b65\u63d0\u4ea4\u8868\u5355<\/p>\n<p>  setTimeout(() =&gt; {<\/p>\n<p>    \/\/ \u5047\u8bbe\u8868\u5355\u63d0\u4ea4\u5b8c\u6210\uff0c\u91cd\u7f6e\u6807\u5fd7<\/p>\n<p>    isSubmitting = false;<\/p>\n<p>  }, 2000);<\/p>\n<p>}<\/p>\n<p>const submitButton = document.querySelector(&#039;#submit-button&#039;);<\/p>\n<p>submitButton.addEventListener(&#039;click&#039;, submitForm);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u9632\u6b62\u5728\u8868\u5355\u63d0\u4ea4\u7684\u8fc7\u7a0b\u4e2d\u53d1\u8d77\u65b0\u7684\u63d0\u4ea4\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u65f6\u95f4\u6233\u6216\u8ba1\u6570\u5668<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u65f6\u95f4\u6233\u6216\u8ba1\u6570\u5668\u4e5f\u662f\u963b\u6b62\u8868\u5355\u91cd\u590d\u63d0\u4ea4\u7684\u6709\u6548\u65b9\u6cd5\u3002\u6bcf\u6b21\u63d0\u4ea4\u65f6\u8bb0\u5f55\u4e0b\u5f53\u524d\u65f6\u95f4\u6233\u6216\u66f4\u65b0\u8ba1\u6570\u5668\uff0c\u6bd4\u8f83\u4e24\u6b21\u63d0\u4ea4\u4e4b\u95f4\u7684\u65f6\u95f4\u95f4\u9694\u6216\u503c\u7684\u53d8\u5316\uff0c\u4ece\u800c\u51b3\u5b9a\u662f\u5426\u5904\u7406\u5f53\u524d\u63d0\u4ea4\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u64cd\u4f5c<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let lastSubmitTime = 0;<\/p>\n<p>function canSubmit() {<\/p>\n<p>  const now = Date.now();<\/p>\n<p>  if (now - lastSubmitTime &lt; 2000) { \/\/ 2\u79d2\u5185\u4e0d\u5141\u8bb8\u91cd\u590d\u63d0\u4ea4<\/p>\n<p>    return false;<\/p>\n<p>  }<\/p>\n<p>  lastSubmitTime = now;<\/p>\n<p>  return true;<\/p>\n<p>}<\/p>\n<p>const submitButton = document.querySelector(&#039;#submit-button&#039;);<\/p>\n<p>submitButton.addEventListener(&#039;click&#039;, function() {<\/p>\n<p>  if (!canSubmit()) {<\/p>\n<p>    console.log(&quot;Too quick! W<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t a bit.&quot;);<\/p>\n<p>    return;<\/p>\n<p>  }<\/p>\n<p>  \/\/ \u63d0\u4ea4\u8868\u5355\u903b\u8f91<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u670d\u52a1\u5668\u7aef\u9a8c\u8bc1<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u5ba2\u6237\u7aef\u7684\u7b56\u7565\u53ef\u4ee5\u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u51cf\u5c11\u91cd\u590d\u63d0\u4ea4\u7684\u95ee\u9898\uff0c\u4f46\u5b8c\u6574\u7684\u89e3\u51b3\u65b9\u6848\u8fd8\u5e94\u5305\u542b\u670d\u52a1\u5668\u7aef\u9a8c\u8bc1\u3002\u670d\u52a1\u5668\u53ef\u4ee5\u68c0\u67e5\u76f8\u540c\u7528\u6237\u5728\u77ed\u65f6\u95f4\u5185\u7684\u63d0\u4ea4\u9891\u7387\uff0c\u82e5\u53d1\u73b0\u5f02\u5e38\uff0c\u5219\u53ef\u4ee5\u62d2\u7edd\u540e\u7eed\u7684\u63d0\u4ea4\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<p><p><strong>\u670d\u52a1\u5668\u7aef\u5904\u7406\u793a\u4f8b<\/strong>\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u4f1a\u8bdd\u6216\u7528\u6237ID\u8ffd\u8e2a\u6bcf\u6b21\u8bf7\u6c42\u3002<\/li>\n<li>\u68c0\u67e5\u76f8\u540cID\u5728\u89c4\u5b9a\u65f6\u95f4\u5185\u7684\u63d0\u4ea4\u6b21\u6570\u3002<\/li>\n<li>\u82e5\u8bf7\u6c42\u8fc7\u4e8e\u9891\u7e41\uff0c\u5219\u8fd4\u56de\u9519\u8bef\u54cd\u5e94\uff0c\u63d0\u793a\u7528\u6237\u7a0d\u540e\u518d\u8bd5\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u6837\u7684\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u5668\u7aef\u7684\u53cc\u91cd\u9a8c\u8bc1\uff0c\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u751a\u81f3\u675c\u7edd\u8868\u5355\u7684\u91cd\u590d\u63d0\u4ea4\u95ee\u9898\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u548c\u7cfb\u7edf\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4e3a\u4ec0\u4e48\u524d\u7aef JavaScript\u9700\u8981\u9632\u6b62\u91cd\u590d\u63d0\u4ea4\u8868\u5355\uff1f<\/strong><\/p>\n<p>\u91cd\u590d\u63d0\u4ea4\u8868\u5355\u53ef\u80fd\u5bfc\u81f4\u91cd\u590d\u7684\u6570\u636e\u4fdd\u5b58\u6216\u8005\u591a\u6b21\u6267\u884c\u540c\u4e00\u4e2a\u64cd\u4f5c\uff0c\u5bf9\u7528\u6237\u4f53\u9a8c\u548c\u6570\u636e\u7684\u51c6\u786e\u6027\u90fd\u4f1a\u9020\u6210\u5f71\u54cd\u3002\u56e0\u6b64\uff0c\u524d\u7aef JavaScript\u7684\u91cd\u590d\u63d0\u4ea4\u8868\u5355\u7684\u9632\u6b62\u5c31\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<p><strong>2. \u5728\u524d\u7aef JavaScript\u5982\u4f55\u9632\u6b62\u91cd\u590d\u63d0\u4ea4\u8868\u5355\uff1f<\/strong><\/p>\n<p>\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u9632\u6b62\u91cd\u590d\u63d0\u4ea4\u8868\u5355\u7684\u95ee\u9898\uff0c\u5728\u524d\u7aef JavaScript\u4e2d\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\u7981\u7528\u63d0\u4ea4\u6309\u94ae\uff1a\u5728\u8868\u5355\u63d0\u4ea4\u540e\uff0c\u7acb\u5373\u7981\u7528\u63d0\u4ea4\u6309\u94ae\uff0c\u9632\u6b62\u7528\u6237\u591a\u6b21\u70b9\u51fb\u3002\u53ef\u4ee5\u901a\u8fc7\u7ed9\u6309\u94ae\u8bbe\u7f6edisabled\u5c5e\u6027\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">\/\/ Example\ndocument.getElementById(&quot;submitBtn&quot;).disabled = true;\n<\/code><\/pre>\n<ul>\n<li>\u8868\u5355\u9a8c\u8bc1\uff1a\u5728\u524d\u7aef\u8fdb\u884c\u5fc5\u8981\u7684\u8868\u5355\u9a8c\u8bc1\uff0c\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u5b8c\u6574\u6027\u3002\u901a\u8fc7\u9a8c\u8bc1\u7528\u6237\u7684\u8f93\u5165\uff0c\u53ea\u6709\u5728\u8868\u5355\u6570\u636e\u7b26\u5408\u9884\u671f\u65f6\u624d\u5141\u8bb8\u63d0\u4ea4\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-javascript\">\/\/ Example\nvar form = document.getElementById(&quot;myForm&quot;);\nform.addEventListener(&quot;submit&quot;, function(event) {\n    event.preventDefault(); \/\/ \u963b\u6b62\u8868\u5355\u7684\u9ed8\u8ba4\u63d0\u4ea4\u884c\u4e3a\n    if (validateForm()) {\n        form.submit(); \/\/ \u8868\u5355\u9a8c\u8bc1\u901a\u8fc7\u540e\u518d\u8fdb\u884c\u63d0\u4ea4\n    }\n});\n\nfunction validateForm() {\n    \/\/ \u8fdb\u884c\u8868\u5355\u9a8c\u8bc1\u7684\u903b\u8f91\n    \/\/ \u8fd4\u56detrue\u8868\u793a\u9a8c\u8bc1\u901a\u8fc7\uff0c\u5141\u8bb8\u63d0\u4ea4\n    \/\/ \u8fd4\u56defalse\u8868\u793a\u9a8c\u8bc1\u672a\u901a\u8fc7\uff0c\u963b\u6b62\u63d0\u4ea4\n}\n<\/code><\/pre>\n<p><strong>3. \u4f7f\u7528\u9632\u6296\u8282\u6d41\u51fd\u6570\uff1a<\/strong><\/p>\n<p>\u9632\u6296\u548c\u8282\u6d41\u51fd\u6570\u662f\u4e00\u79cd\u5e38\u7528\u7684\u524d\u7aef\u6280\u672f\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u9650\u5236\u51fd\u6570\u7684\u6267\u884c\u9891\u7387\u3002\u5728\u8868\u5355\u63d0\u4ea4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u6765\u9632\u6b62\u91cd\u590d\u63d0\u4ea4\u3002\u9632\u6296\u51fd\u6570\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u53ea\u6267\u884c\u4e00\u6b21\uff0c\u8282\u6d41\u51fd\u6570\u53ef\u4ee5\u6bcf\u9694\u4e00\u5b9a\u65f6\u95f4\u6267\u884c\u4e00\u6b21\u3002<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u9632\u6296\u51fd\u6570\nfunction debounce(fn, delay) {\n    let timer = null;\n    return function() {\n        clearTimeout(timer);\n        timer = setTimeout(() =&gt; {\n            fn.apply(this, arguments);\n        }, delay);\n    }\n}\n\n\/\/ \u8282\u6d41\u51fd\u6570\nfunction throttle(fn, interval) {\n    let last = 0;\n    return function() {\n        const now = Date.now();\n        if (now - last &gt;= interval) {\n            fn.apply(this, arguments);\n            last = now;\n        }\n    }\n}\n\n\/\/ Example\nvar submitForm = debounce(function() {\n    \/\/ \u8868\u5355\u63d0\u4ea4\u903b\u8f91\n}, 2000);\n\ndocument.getElementById(&quot;myForm&quot;).addEventListener(&quot;submit&quot;, function(event) {\n    event.preventDefault();\n    submitForm();\n});\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u4e4b\u4e00\u6216\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5f88\u597d\u5730\u9632\u6b62\u524d\u7aef JavaScript\u91cd\u590d\u63d0\u4ea4\u8868\u5355\u7684\u95ee\u9898\u3002\u8bb0\u4f4f\uff0c\u4fdd\u8bc1\u7528\u6237\u4f53\u9a8c\u548c\u6570\u636e\u7684\u51c6\u786e\u6027\u662f\u524d\u7aef\u5f00\u53d1\u4e2d\u7684\u91cd\u8981\u4efb\u52a1\u4e4b\u4e00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript\u5728\u524d\u7aef\u5f00\u53d1\u4e2d\u626e\u6f14\u7740\u81f3\u5173\u91cd\u8981\u7684\u89d2\u8272\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u8868\u5355\u63d0\u4ea4\u65f6\u3002\u4e3a\u4e86\u9632\u6b62\u91cd\u590d\u63d0\u4ea4\u8868\u5355\uff0c\u53ef\u4ee5\u91c7\u53d6\u51e0 [&hellip;]","protected":false},"author":3,"featured_media":297473,"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\/297444"}],"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=297444"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/297444\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/297473"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=297444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=297444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=297444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}