{"id":213,"date":"2025-01-01T21:04:00","date_gmt":"2025-01-01T13:04:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=213"},"modified":"2026-06-04T21:11:40","modified_gmt":"2026-06-04T13:11:40","slug":"%e5%87%bd%e6%95%b0%e9%98%b2%e6%8a%96%e5%92%8c%e8%8a%82%e6%b5%81","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2025\/01\/01\/%e5%87%bd%e6%95%b0%e9%98%b2%e6%8a%96%e5%92%8c%e8%8a%82%e6%b5%81\/","title":{"rendered":"\u51fd\u6570\u9632\u6296\u548c\u8282\u6d41"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u51fd\u6570\u9632\u6296<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u51fd\u6570\u9632\u6296\uff0c\u662f\u6307\u9632\u6b62\u51fd\u6570\u5728\u6781\u77ed\u7684\u65f6\u95f4\u5185\u53cd\u590d\u8c03\u7528\uff0c\u9020\u6210\u8d44\u6e90\u7684\u6d6a\u8d39<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u9632\u6296\uff0c\u5176\u5b9e\u5c31\u662f\u9632\u6b62\u51fd\u6570\u77ed\u65f6\u95f4\u5185\u88ab\u8c03\u7528\u591a\u6b21\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u5b8c\u6210\u8be5\u51fd\u6570\uff0c\u9700\u8981\u7ed9\u4e88\u4e24\u4e2a\u6761\u4ef6\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u544a\u8bc9\u6211\u4e00\u6bb5\u65f6\u95f4\u540e\u8981\u505a\u4ec0\u4e48\u4e8b\uff08\u8fd9\u91cc\u5e94\u8be5\u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5373\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff09<\/li>\n\n\n\n<li>\u544a\u8bc9\u6211\u8981\u7b49\u5f85\u591a\u957f\u65f6\u95f4\uff08\u6beb\u79d2\uff09<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u9632\u6296\u7684\u901a\u7528\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**<br> * \u51fd\u6570\u9632\u6296<br> * @param {function} func \u4e00\u6bb5\u65f6\u95f4\u540e\uff0c\u8981\u8c03\u7528\u7684\u51fd\u6570<br> * @param {number} wait \u7b49\u5f85\u7684\u65f6\u95f4\uff0c\u5355\u4f4d\u6beb\u79d2<br> *\/<br>function debounce(func, wait) {<br> &nbsp; &nbsp;\/\/ \u8bbe\u7f6e\u53d8\u91cf\uff0c\u8bb0\u5f55 setTimeout \u5f97\u5230\u7684 id<br> &nbsp; &nbsp;var timerId = null;<br> &nbsp; &nbsp;return function (...args) {<br> &nbsp; &nbsp; &nbsp; &nbsp;if (timerId) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u5982\u679c\u6709\u503c\uff0c\u8bf4\u660e\u76ee\u524d\u6b63\u5728\u7b49\u5f85\u4e2d\uff0c\u6e05\u9664\u5b83<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;clearTimeout(timerId);<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u91cd\u65b0\u5f00\u59cb\u8ba1\u65f6<br> &nbsp; &nbsp; &nbsp; &nbsp;timerId = setTimeout(() =&gt; {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;func(...args);<br> &nbsp; &nbsp; &nbsp;  }, wait);<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u51fd\u6570\u8282\u6d41<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u8282\u6d41\u7684\u76ee\u7684\uff0c\u4e5f\u662f\u4e3a\u4e86\u9632\u6b62\u4e00\u4e2a\u51fd\u6570\u77ed\u65f6\u95f4\u5185\u88ab\u9891\u7e41\u7684\u89e6\u53d1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u548c\u51fd\u6570\u9632\u6296\u7684\u539f\u7406\u4e0d\u540c\uff0c\u51fd\u6570\u8282\u6d41\u7684\u6838\u5fc3\u601d\u60f3\u662f\u8ba9\u8fde\u7eed\u7684\u51fd\u6570\u6267\u884c\uff0c\u53d8\u4e3a\u56fa\u5b9a\u65f6\u95f4\u6bb5\u95f4\u65ad\u5730\u6267\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5173\u4e8e\u8282\u6d41\u7684\u5b9e\u73b0\uff0c\u6709 <em>2<\/em> \u79cd\u4e3b\u6d41\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4e00\u79cd\u662f<strong>\u4f7f\u7528\u65f6\u95f4\u6233<\/strong>\uff0c\u4e00\u79cd\u662f<strong>\u8bbe\u7f6e\u5b9a\u65f6\u5668<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\uff081\uff09\u4f7f\u7528\u65f6\u95f4\u6233<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u89e6\u53d1\u4e8b\u4ef6\u65f6\uff0c\u53d6\u51fa\u5f53\u524d\u7684\u65f6\u95f4\u6233\uff0c\u7136\u540e\u51cf\u53bb\u4e4b\u524d\u7684\u65f6\u95f4\u6233\uff08\u6700\u4e00\u5f00\u59cb\u503c\u8bbe\u4e3a <em>0<\/em>\uff09\uff0c\u5982\u679c\u5927\u4e8e\u8bbe\u7f6e\u7684\u65f6\u95f4\u5468\u671f\uff0c\u5c31\u6267\u884c\u51fd\u6570\uff0c\u7136\u540e\u66f4\u65b0\u65f6\u95f4\u6233\u4e3a\u5f53\u524d\u7684\u65f6\u95f4\u6233\uff0c\u5982\u679c\u5c0f\u4e8e\uff0c\u5c31\u4e0d\u6267\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f\u5c01\u88c5\u4f7f\u7528\u65f6\u95f4\u6233\u7684\u51fd\u6570\u8282\u6d41\u7684\u901a\u7528\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**<br> * <br> * @param {\u8981\u8fdb\u884c\u8282\u6d41\u7684\u51fd\u6570} func <br> * @param {\u95f4\u9694\u65f6\u95f4} wait <br> * @returns <br> *\/<br>function throttle(func, wait) {<br> &nbsp; &nbsp;var args; \/\/ \u5b58\u50a8\u51fd\u6570\u53c2\u6570<br> &nbsp; &nbsp;var previous = 0; \/\/ \u4e00\u5f00\u59cb\u7684\u9ed8\u8ba4\u65f6\u95f4<br> &nbsp; &nbsp;return function () {<br> &nbsp; &nbsp; &nbsp; &nbsp;var now = new Date(); \/\/ \u83b7\u53d6\u6700\u65b0\u7684\u65f6\u95f4\u6233<br> &nbsp; &nbsp; &nbsp; &nbsp;args = arguments; \/\/ \u83b7\u53d6\u53c2\u6570<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u8fdb\u884c\u65f6\u95f4\u6233\u7684\u5224\u65ad\uff0c\u5982\u679c\u8d85\u51fa\u89c4\u5b9a\u65f6\u95f4\uff0c\u5219\u6267\u884c<br> &nbsp; &nbsp; &nbsp; &nbsp;if (now - previous &gt; wait) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;func.apply(null, args);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;previous = now;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\uff082\uff09\u8bbe\u7f6e\u5b9a\u65f6\u5668<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u662f\u8bbe\u7f6e\u5b9a\u65f6\u5668\uff0c\u89e6\u53d1\u4e8b\u4ef6\u65f6\u8bbe\u7f6e\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u518d\u89e6\u53d1\u4e8b\u4ef6\u7684\u65f6\u5019\uff0c\u5982\u679c\u5b9a\u65f6\u5668\u5b58\u5728\uff0c\u5c31\u4e0d\u6267\u884c\uff0c\u76f4\u5230\u5b9a\u65f6\u5668\u6267\u884c\uff0c\u7136\u540e\u6267\u884c\u51fd\u6570\uff0c\u6e05\u7a7a\u5b9a\u65f6\u5668\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u8bbe\u7f6e\u4e0b\u4e2a\u5b9a\u65f6\u5668\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f\u5c01\u88c5\u4f7f\u7528\u5b9a\u65f6\u5668\u7684\u51fd\u6570\u8282\u6d41\u7684\u901a\u7528\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**<br> * <br> * @param {\u8981\u8282\u6d41\u6267\u884c\u7684\u51fd\u6570} func <br> * @param {\u8282\u6d41\u7684\u65f6\u95f4\u95f4\u9694} wait <br> * @returns <br> *\/<br>function throttle(func, wait) {<br> &nbsp; &nbsp;\/\/ timeout \u5b58\u50a8\u8ba1\u65f6\u5668\u8fd4\u56de\u503c<br> &nbsp; &nbsp;\/\/ args \u5b58\u50a8\u53c2\u6570<br> &nbsp; &nbsp;var timeout, args;<br> &nbsp; &nbsp;return function () {<br> &nbsp; &nbsp; &nbsp; &nbsp;args = arguments;<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u5982\u679c timeout \u6709\u503c\uff0c\u8bf4\u660e\u4e0a\u4e00\u6b21\u7684\u6267\u884c\u95f4\u9694\u65f6\u95f4\u8fd8\u6ca1\u8fc7<br> &nbsp; &nbsp; &nbsp; &nbsp;if (!timeout) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u8fdb\u5165\u6b64 if \u8bf4\u660e\u65f6\u95f4\u95f4\u9694\u5df2\u7ecf\u8fc7\u4e86<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u5148\u6267\u884c\u4e00\u6b21\u8981\u6267\u884c\u7684\u51fd\u6570<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;func.apply(null, args)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u7136\u540e\u91cd\u65b0\u8bbe\u7f6e\u65f6\u95f4\u95f4\u9694<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;timeout = setTimeout(function () {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;timeout = null;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }, wait);<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u51fd\u6570\u9632\u6296 \u51fd\u6570\u9632\u6296\uff0c\u662f\u6307\u9632\u6b62\u51fd\u6570\u5728\u6781\u77ed\u7684\u65f6\u95f4\u5185\u53cd\u590d..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,11],"tags":[],"class_list":["post-213","post","type-post","status-publish","format-standard","hentry","category-js","category-front"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/js\/\" rel=\"category tag\">JS<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/\" rel=\"category tag\">\u524d\u7aef<\/a>","tag_info":"\u524d\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=213"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":214,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/213\/revisions\/214"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}