{"id":31179,"date":"2024-11-25T16:12:33","date_gmt":"2024-11-25T08:12:33","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31179\/"},"modified":"2024-11-25T16:12:33","modified_gmt":"2024-11-25T08:12:33","slug":"%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e5%87%bd%e6%95%b0%e8%8a%82%e6%b5%81%e5%a4%9a%e6%ac%a1%e7%82%b9%e5%87%bb%e8%b7%b3%e8%bd%ac%e5%a6%82%e4%bd%95%e9%98%b2%e6%ad%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31179\/","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u51fd\u6570\u8282\u6d41\u591a\u6b21\u70b9\u51fb\u8df3\u8f6c\u5982\u4f55\u9632\u6b62"},"content":{"rendered":"<p style=\"margin: 20px 0px;font-weight: 400;font-size: 24px;line-height: inherit;font-family: -apple-system, BlinkMacSystemFont,\"><span style=\"font-size: 14px\">\u5728\u5b66\u672c\u6587\u7684\u65f6\u5019\u6211\u4eec\u8981\u77e5\u9053\u51fd\u6570\u8282\u6d41\u662f\u4ec0\u4e48\uff0c<\/span><span style=\"font-size: 14px\">\uff0c\u63a5\u4e0b\u6765\u672c\u6587\u5c31\u4e3b\u8981\u548c\u5927\u5bb6\u5206\u4eab\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u51fd\u6570\u8282\u6d41\u591a\u6b21\u70b9\u51fb\u8df3\u8f6c\u5982\u4f55\u9632\u6b62\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u7528\u3002<\/span><\/p>\n<p style=\"margin: 20px 0px;font-weight: 400;font-size: 24px;line-height: inherit;font-family: -apple-system, BlinkMacSystemFont,\"><strong><span style=\"font-size: 14px\">\u573a\u666f<\/span><\/strong><\/p>\n<p><span style=\"font-size: 14px\">\u5728\u4f7f\u7528\u5c0f\u7a0b\u5e8f\u7684\u65f6\u5019\u4f1a\u51fa\u73b0\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff1a\u5f53\u7f51\u7edc\u6761\u4ef6\u5dee\u6216\u5361\u987f\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u8005\u4f1a\u8ba4\u4e3a\u70b9\u51fb\u65e0\u6548\u800c\u8fdb\u884c\u591a\u6b21\u70b9\u51fb\uff0c\u6700\u540e\u51fa\u73b0\u591a\u6b21\u8df3\u8f6c\u9875\u9762\u7684\u60c5\u51b5\u3002<\/span><\/p>\n<p style=\"margin: 20px 0px;font-weight: 400;font-size: 24px;line-height: inherit;font-family: -apple-system, BlinkMacSystemFont,\"><strong><span style=\"font-size: 14px\">\u89e3\u51b3\u529e\u6cd5<\/span><\/strong><\/p>\n<p><span style=\"font-size: 14px\">\u7136\u540e\u4ece&nbsp;\u8f7b\u677e\u7406\u89e3JS\u51fd\u6570\u8282\u6d41\u548c\u51fd\u6570\u9632\u6296&nbsp;\u4e2d\u627e\u5230\u4e86\u89e3\u51b3\u529e\u6cd5\uff0c\u5c31\u662f\u51fd\u6570\u8282\u6d41(throttle)\uff1a\u51fd\u6570\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u591a\u6b21\u89e6\u53d1\u53ea\u4f1a\u6267\u884c\u7b2c\u4e00\u6b21\uff0c\u5728\u8fd9\u6bb5\u65f6\u95f4\u7ed3\u675f\u524d\uff0c\u4e0d\u7ba1\u89e6\u53d1\u591a\u5c11\u6b21\u4e5f\u4e0d\u4f1a\u6267\u884c\u51fd\u6570\u3002<\/span><\/p>\n<p><span style=\"font-size: 14px\">\/utils\/util.js:<\/span><\/p>\n<pre>&lt;span style=\"font-size: 14px;\"&gt;function throttle(fn, gapTime) {&lt;br&gt; &nbsp; &nbsp;if (gapTime == null || gapTime == undefined) {&lt;br&gt; &nbsp; &nbsp; &nbsp; &nbsp;gapTime = 1500&lt;br&gt; &nbsp; &nbsp;}&lt;br&gt; &nbsp; &nbsp;let _lastTime = null return function () {&lt;br&gt; &nbsp; &nbsp; &nbsp; &nbsp;let _nowTime = + new Date()&lt;br&gt; &nbsp; &nbsp; &nbsp; &nbsp;if (_nowTime - _lastTime &amp;gt; gapTime || !_lastTime) {&lt;br&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fn()&lt;br&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_lastTime = _nowTime&lt;br&gt; &nbsp; &nbsp; &nbsp; &nbsp;}&lt;br&gt; &nbsp; &nbsp;}&lt;br&gt;}&lt;br&gt;module.exports = {&lt;br&gt; &nbsp;throttle: throttle&lt;br&gt;}&lt;br&gt;\/pages\/throttle\/throttle.wxml:&lt;br&gt;&lt;button bindtap=\"'tap'\" data-key=\"'abc'\"&gt;tap&lt;\/button&gt;&lt;br&gt;\/pages\/throttle\/throttle.js&lt;br&gt;const util = require('..\/..\/utils\/util.js')&lt;br&gt;Page({&lt;br&gt; &nbsp; &nbsp;data: {&lt;br&gt; &nbsp; &nbsp; &nbsp; &nbsp;text: 'tomfriwel'&lt;br&gt; &nbsp; &nbsp;},&lt;br&gt; &nbsp; &nbsp;onLoad: function (options) {&lt;br&gt; &nbsp; &nbsp;},&lt;br&gt; &nbsp; &nbsp;tap: util.throttle(function (e) {&lt;br&gt; &nbsp; &nbsp; &nbsp; &nbsp;console.log(this)&lt;br&gt; &nbsp; &nbsp; &nbsp; &nbsp;console.log(e)&lt;br&gt; &nbsp; &nbsp; &nbsp; &nbsp;console.log((new Date()).getSeconds())&lt;br&gt; &nbsp; &nbsp;}, 1000)&lt;br&gt;})&lt;\/span&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><span style=\"font-size: 14px\">\u8fd9\u6837\uff0c\u75af\u72c2\u70b9\u51fb\u6309\u94ae\u4e5f\u53ea\u4f1a1s\u89e6\u53d1\u4e00\u6b21\u3002<\/span><\/p>\n<p><span style=\"font-size: 14px\">\u4f46\u662f\u8fd9\u6837\u7684\u8bdd\u51fa\u73b0\u4e00\u4e2a\u95ee\u9898\uff0c\u5c31\u662f\u5f53\u4f60\u60f3\u8981\u83b7\u53d6this.data\u5f97\u5230\u7684this\u662fundefined, \u6216\u8005\u60f3\u8981\u83b7\u53d6\u5fae\u4fe1\u7ec4\u4ef6button\u4f20\u9012\u7ed9\u70b9\u51fb\u51fd\u6570\u7684\u6570\u636ee\u4e5f\u662fundefined\uff0c\u6240\u4ee5throttle\u51fd\u6570\u8fd8\u9700\u8981\u505a\u4e00\u70b9\u5904\u7406\u6765\u4f7f\u5176\u80fd\u7528\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684\u9875\u9762js\u91cc\u3002<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/\/upload\/image\/170\/346\/894\/1517020368182657.jpg\" class=\"aligncenter\" title=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u51fd\u6570\u8282\u6d41\u591a\u6b21\u70b9\u51fb\u8df3\u8f6c\u5982\u4f55\u9632\u6b62\u63d2\u56fe\" alt=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u51fd\u6570\u8282\u6d41\u591a\u6b21\u70b9\u51fb\u8df3\u8f6c\u5982\u4f55\u9632\u6b62\u63d2\u56fe\" \/><\/p>\n<p><span style=\"font-size: 14px\">\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\u7684\u539f\u56e0\u662fthrottle\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65b0\u51fd\u6570\uff0c\u5df2\u7ecf\u4e0d\u662f\u6700\u521d\u7684\u51fd\u6570\u4e86\u3002\u65b0\u51fd\u6570\u5305\u88f9\u7740\u539f\u51fd\u6570\uff0c\u6240\u4ee5\u7ec4\u4ef6button\u4f20\u9012\u7684\u53c2\u6570\u662f\u5728\u65b0\u51fd\u6570\u91cc\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u628a\u8fd9\u4e9b\u53c2\u6570\u4f20\u9012\u7ed9\u771f\u6b63\u9700\u8981\u6267\u884c\u7684\u51fd\u6570fn\u3002<\/span><\/p>\n<p><span style=\"font-size: 14px\">\u6700\u540e\u7684throttle\u51fd\u6570\u5982\u4e0b\uff1a<\/span><\/p>\n<pre>&lt;span style=\"font-size: 14px;\"&gt;function&nbsp;throttle(fn,&nbsp;gapTime)&nbsp;{&lt;br&gt;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(gapTime&nbsp;==&nbsp;null&nbsp;||&nbsp;gapTime&nbsp;==&nbsp;undefined)&nbsp;{&lt;br&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gapTime&nbsp;=&nbsp;1500&lt;br&gt;&nbsp;&nbsp;&nbsp;&nbsp;}&lt;br&gt;&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;_lastTime&nbsp;=&nbsp;null&nbsp;\/\/&nbsp;\u8fd4\u56de\u65b0\u7684\u51fd\u6570&nbsp;return&nbsp;function&nbsp;()&nbsp;{&lt;br&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;_nowTime&nbsp;=&nbsp;+&nbsp;new&nbsp;Date()&lt;br&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(_nowTime&nbsp;-&nbsp;_lastTime&nbsp;&amp;gt;&nbsp;gapTime&nbsp;||&nbsp;!_lastTime)&nbsp;{&lt;br&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn.apply(this,&nbsp;arguments)&nbsp;&nbsp;&nbsp;\/\/\u5c06this\u548c\u53c2\u6570\u4f20\u7ed9\u539f\u51fd\u6570&lt;br&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_lastTime&nbsp;=&nbsp;_nowTime&lt;br&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&lt;br&gt;&nbsp;&nbsp;&nbsp;&nbsp;}&lt;br&gt;}&lt;br&gt;&lt;\/span&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><span style=\"font-size: 14px\">\u518d\u6b21\u70b9\u51fb\u6309\u94aethis\u548ce\u90fd\u6709\u4e86\uff1a<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/\/upload\/image\/739\/515\/936\/1517020380987780.jpg\" class=\"aligncenter\" title=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u51fd\u6570\u8282\u6d41\u591a\u6b21\u70b9\u51fb\u8df3\u8f6c\u5982\u4f55\u9632\u6b62\u63d2\u56fe1\" alt=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u51fd\u6570\u8282\u6d41\u591a\u6b21\u70b9\u51fb\u8df3\u8f6c\u5982\u4f55\u9632\u6b62\u63d2\u56fe1\" \/><\/p>\n<p><span style=\"font-size: 14px\">\u76f8\u5173\u63a8\u8350\uff1a<\/span><\/p>\n<\/p>\n<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u51fd\u6570\u8282\u6d41\u591a\u6b21\u70b9\u51fb\u8df3\u8f6c\u5982\u4f55\u9632\u6b62\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5b66\u672c\u6587\u7684\u65f6\u5019\u6211\u4eec\u8981\u77e5\u9053\u51fd\u6570\u8282\u6d41\u662f\u4ec0\u4e48\uff0c\uff0c\u63a5\u4e0b\u6765\u672c\u6587\u5c31\u4e3b\u8981\u548c\u5927\u5bb6\u5206\u4eab\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u51fd\u6570\u8282\u6d41\u591a\u6b21\u70b9\u51fb\u8df3\u8f6c\u5982\u4f55\u9632\u6b62\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u7528\u3002 \u573a\u666f \u5728\u4f7f\u7528\u5c0f\u7a0b\u5e8f\u7684\u65f6\u5019\u4f1a\u51fa\u73b0\u8fd9\u6837\u4e00\u79cd\u60c5\u51b5\uff1a\u5f53\u7f51\u7edc\u6761\u4ef6\u5dee\u6216\u5361\u987f\u7684\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u8005\u4f1a\u8ba4\u4e3a\u70b9\u51fb\u65e0\u6548\u800c\u8fdb\u884c\u591a\u6b21\u70b9\u51fb\uff0c\u6700\u540e\u51fa\u73b0\u591a\u6b21\u8df3\u8f6c\u9875\u9762\u7684\u60c5\u51b5\u3002 \u89e3\u51b3\u529e\u6cd5 \u7136\u540e\u4ece&nbsp;\u8f7b\u677e\u7406\u89e3JS\u51fd\u6570\u8282\u6d41\u548c\u51fd\u6570\u9632\u6296&nbsp;\u4e2d\u627e\u5230\u4e86\u89e3\u51b3\u529e\u6cd5\uff0c\u5c31\u662f\u51fd\u6570\u8282\u6d41(throttle)\uff1a\u51fd\u6570\u5728\u4e00\u6bb5\u65f6\u95f4\u5185\u591a\u6b21\u89e6\u53d1\u53ea\u4f1a\u6267\u884c\u7b2c\u4e00\u6b21\uff0c\u5728\u8fd9\u6bb5\u65f6\u95f4\u7ed3\u675f\u524d\uff0c\u4e0d\u7ba1\u89e6\u53d1\u591a\u5c11\u6b21\u4e5f\u4e0d\u4f1a\u6267\u884c\u51fd\u6570\u3002 \/utils\/util.js: &lt;span style=&#8221;font-size: 14px;&#8221;&gt;function throttle(fn, gapTime) {&lt;br&gt; &nbsp; &nbsp;if (gapTime == null || gapTime == undefined) {&lt;br&gt; &nbsp; &nbsp; &nbsp; &nbsp;gapTime = 1500&lt;br&gt; &nbsp; &nbsp;}&lt;br&gt; &nbsp; &nbsp;let _lastTime = null return function () {&lt;br&gt; &nbsp; &nbsp; &nbsp; &nbsp;let _nowTime = + new Date()&lt;br&gt; &nbsp; &nbsp; &nbsp; &nbsp;if (_nowTime &#8211; _lastTime [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-31179","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=31179"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31179\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}