{"id":267,"date":"2017-05-11T19:17:43","date_gmt":"2017-05-11T11:17:43","guid":{"rendered":"http:\/\/yusemo.com\/?p=267"},"modified":"2017-12-09T15:09:59","modified_gmt":"2017-12-09T07:09:59","slug":"%e5%87%bd%e6%95%b0%e8%8a%82%e6%b5%81%ef%bc%88throttle%ef%bc%89%e4%b8%8e%e5%87%bd%e6%95%b0%e5%8e%bb%e6%8a%96%ef%bc%88debounce%ef%bc%89","status":"publish","type":"post","link":"https:\/\/yusemo.com\/?p=267","title":{"rendered":"\u51fd\u6570\u8282\u6d41\uff08throttle\uff09\u4e0e\u51fd\u6570\u53bb\u6296\uff08debounce\uff09"},"content":{"rendered":"<p>\u9879\u76ee\u4f18\u5316\u79cd\u53d1\u73b0\u5f88\u591a\u65f6\u5019\u9700\u8981\u8f93\u5165\u4e4b\u540e\u9a8c\u8bc1\uff0c\u56e0\u6b64\u7528\u5f97\u9891\u7387\u6bd4\u8f83\u9ad8\u76842\u4e2a\u51fd\u6570debounce\u548cthrottle\u3002\u8fd8\u6709\u4ee5\u4e0b\u573a\u666f\u5f80\u5f80\u7531\u4e8e\u4e8b\u4ef6\u9891\u7e41\u88ab\u89e6\u53d1\uff0c\u56e0\u800c\u9891\u7e41\u6267\u884cDOM\u64cd\u4f5c\u3001\u8d44\u6e90\u52a0\u8f7d\u7b49\u91cd\u884c\u4e3a\uff0c\u5bfc\u81f4UI\u505c\u987f\u751a\u81f3\u6d4f\u89c8\u5668\u5d29\u6e83\u3002<\/p>\n<p>1. window\u5bf9\u8c61\u7684resize\u3001scroll\u4e8b\u4ef6<\/p>\n<p>2. \u62d6\u62fd\u65f6\u7684mousemove\u4e8b\u4ef6<\/p>\n<p>3. \u5c04\u51fb\u6e38\u620f\u4e2d\u7684mousedown\u3001keydown\u4e8b\u4ef6<\/p>\n<p>4. \u6587\u5b57\u8f93\u5165\u3001\u81ea\u52a8\u5b8c\u6210\u7684keyup\u4e8b\u4ef6<\/p>\n<p>\u5b9e\u9645\u4e0a\u5bf9\u4e8ewindow\u7684resize\u4e8b\u4ef6\uff0c\u5b9e\u9645\u9700\u6c42\u5927\u591a\u4e3a\u505c\u6b62\u6539\u53d8\u5927\u5c0fn\u6beb\u79d2\u540e\u6267\u884c\u540e\u7eed\u5904\u7406\uff1b\u800c\u5176\u4ed6\u4e8b\u4ef6\u5927\u591a\u7684\u9700\u6c42\u662f\u4ee5\u4e00\u5b9a\u7684\u9891\u7387\u6267\u884c\u540e\u7eed\u5904\u7406\u3002\u9488\u5bf9\u8fd9\u4e24\u79cd\u9700\u6c42\u5c31\u51fa\u73b0\u4e86debounce\u548cthrottle\u4e24\u79cd\u89e3\u51b3\u529e\u6cd5\u3002<\/p>\n<p><strong>debounce<\/strong><\/p>\n<p>debounce\u7684\u5173\u6ce8\u70b9\u662f\u7a7a\u95f2\u7684\u95f4\u9694\u65f6\u95f4<\/p>\n<pre> \/\/ \u51fd\u6570\u53bb\u6296\uff08\u8fde\u7eed\u4e8b\u4ef6\u89e6\u53d1\u7ed3\u675f\u540e\u53ea\u89e6\u53d1\u4e00\u6b21\uff09\r\n \/\/ sample 1: _.debounce(function(){}, 1000)\r\n \/\/ \u8fde\u7eed\u4e8b\u4ef6\u7ed3\u675f\u540e\u7684 1000ms \u540e\u89e6\u53d1\r\n \/\/ sample 1: _.debounce(function(){}, 1000, true)\r\n \/\/ \u8fde\u7eed\u4e8b\u4ef6\u89e6\u53d1\u540e\u7acb\u5373\u89e6\u53d1\uff08\u6b64\u65f6\u4f1a\u5ffd\u7565\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff09\r\n\r\n_.debounce = function(func, wait, immediate) {\r\n    \/\/ immediate\u9ed8\u8ba4\u4e3afalse\r\n    var timeout, args, context, timestamp, result;\r\n\r\n    var later = function() {\r\n      \/\/ \u5f53wait\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u671f\u95f4\u591a\u6b21\u8c03\u7528_.debounce\u8fd4\u56de\u7684\u51fd\u6570\uff0c\u5219\u4f1a\u4e0d\u65ad\u66f4\u65b0timestamp\u7684\u503c\uff0c\u5bfc\u81f4last &lt; wait &amp;&amp; last &gt;= 0\u4e00\u76f4\u4e3atrue\uff0c\u4ece\u800c\u4e0d\u65ad\u542f\u52a8\u65b0\u7684\u8ba1\u65f6\u5668\u5ef6\u65f6\u6267\u884cfunc\r\n      var last = _.now() - timestamp;\r\n\r\n      if (last &lt; wait &amp;&amp; last &gt;= 0) {\r\n        timeout = setTimeout(later, wait - last);\r\n      } else {\r\n        timeout = null;\r\n        if (!immediate) {\r\n          result = func.apply(context, args);\r\n          if (!timeout) context = args = null;\r\n        }\r\n      }\r\n    };\r\n\r\n    return function() {\r\n      context = this;\r\n      args = arguments;\r\n      timestamp = _.now();\r\n      \/\/ \u7b2c\u4e00\u6b21\u8c03\u7528\u8be5\u65b9\u6cd5\u65f6\uff0c\u4e14immediate\u4e3atrue\uff0c\u5219\u8c03\u7528func\u51fd\u6570\r\n      var callNow = immediate &amp;&amp; !timeout;\r\n      \/\/ \u5728wait\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u5185\u9996\u6b21\u8c03\u7528\u8be5\u65b9\u6cd5\uff0c\u5219\u542f\u52a8\u8ba1\u65f6\u5668\u5b9a\u65f6\u8c03\u7528func\u51fd\u6570\r\n      if (!timeout) timeout = setTimeout(later, wait);\r\n      if (callNow) {\r\n        result = func.apply(context, args);\r\n        context = args = null;\r\n      }\r\n\r\n      return result;\r\n    };\r\n  };<\/pre>\n<p><strong>Throttle<\/strong><\/p>\n<p>throttle \u7684\u5173\u6ce8\u70b9\u662f\u8fde\u7eed\u7684\u6267\u884c\u95f4\u9694\u65f6\u95f4<\/p>\n<pre><span class=\"pl-c\">\/\/ \u51fd\u6570\u8282\u6d41\uff08\u5982\u679c\u6709\u8fde\u7eed\u4e8b\u4ef6\u54cd\u5e94\uff0c\u5219\u6bcf\u95f4\u9694\u4e00\u5b9a\u65f6\u95f4\u6bb5\u89e6\u53d1\uff09<\/span> \r\n<span class=\"pl-c\">\/\/ \u6bcf\u95f4\u9694 wait(Number) milliseconds \u89e6\u53d1\u4e00\u6b21 func \u65b9\u6cd5\r\n<\/span><span class=\"pl-c\">\/\/ \u5982\u679c options \u53c2\u6570\u4f20\u5165 {leading: false}<\/span> \r\n<span class=\"pl-c\">\/\/ \u90a3\u4e48\u4e0d\u4f1a\u9a6c\u4e0a\u89e6\u53d1\uff08\u7b49\u5f85 wait milliseconds \u540e\u7b2c\u4e00\u6b21\u89e6\u53d1 func\uff09<\/span> <span class=\"pl-c\">\/\/ \u5982\u679c options \u53c2\u6570\u4f20\u5165 {trailing: false}\r\n<\/span><span class=\"pl-c\">\/\/ \u90a3\u4e48\u6700\u540e\u4e00\u6b21\u56de\u8c03\u4e0d\u4f1a\u88ab\u89e6\u53d1\r\n<\/span><span class=\"pl-c\">\/\/ **Notice: options \u4e0d\u80fd\u540c\u65f6\u8bbe\u7f6e leading \u548c trailing \u4e3a false**<\/span> \r\n<span class=\"pl-c\">\/\/ \u793a\u4f8b\uff1a<\/span> \r\n<span class=\"pl-c\">\/\/ var throttled = _.throttle(updatePosition, 100);\r\n<\/span><span class=\"pl-c\">\/\/ $(window).scroll(throttled);<\/span> \r\n<span class=\"pl-c\">\/\/ \u8c03\u7528\u65b9\u5f0f\uff08\u6ce8\u610f\u770b A \u548c B console.log \u6253\u5370\u7684\u4f4d\u7f6e\uff09\uff1a\r\n<\/span><span class=\"pl-c\">\/\/ _.throttle(function, wait, [options])<\/span> \r\n<span class=\"pl-c\">\/\/ sample 1: _.throttle(function(){}, 1000)\r\n<\/span><span class=\"pl-c\">\/\/ print: A, B, B, B ...<\/span> \r\n<span class=\"pl-c\">\/\/ sample 2: _.throttle(function(){}, 1000, {leading: false})<\/span> \r\n<span class=\"pl-c\">\/\/ print: B, B, B, B ...<\/span> \r\n<span class=\"pl-c\">\/\/ sample 3: _.throttle(function(){}, 1000, {trailing: false})<\/span> \r\n<span class=\"pl-c\">\/\/ print: A, A, A, A ...\r\n<\/span> <span class=\"pl-c\">\/\/ ----------------------------------------- \/\/<\/span>\r\n\r\n\r\n_.throttle = function(func, wait, options) { \r\n\r\n\/* options\u7684\u9ed8\u8ba4\u503c * \u8868\u793a\u9996\u6b21\u8c03\u7528\u8fd4\u56de\u503c\u65b9\u6cd5\u65f6\uff0c\u4f1a\u9a6c\u4e0a\u8c03\u7528func\uff1b\u5426\u5219\u4ec5\u4f1a\u8bb0\u5f55\u5f53\u524d\u65f6\u523b\uff0c\u5f53\u7b2c\u4e8c\u6b21\u8c03\u7528\u7684\u65f6\u95f4\u95f4\u9694\u8d85\u8fc7wait\u65f6\uff0c\u624d\u8c03\u7528func\u3002\r\n * options.leading = true; \r\n * \u8868\u793a\u5f53\u8c03\u7528\u65b9\u6cd5\u65f6\uff0c\u672a\u5230\u8fbewait\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\uff0c\u5219\u542f\u52a8\u8ba1\u65f6\u5668\u5ef6\u8fdf\u8c03\u7528func\u51fd\u6570\uff0c\u82e5\u540e\u7eed\u5728\u65e2\u672a\u8fbe\u5230wait\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u548cfunc\u51fd\u6570\u53c8\u672a\u88ab\u8c03\u7528\u7684\u60c5\u51b5\u4e0b\u8c03\u7528\u8fd4\u56de\u503c\u65b9\u6cd5\uff0c\u5219\u88ab\u8c03\u7528\u8bf7\u6c42\u5c06\u88ab\u4e22\u5f03\u3002\r\n * options.trailing = true; \r\n * \u6ce8\u610f\uff1a\u5f53options.trailing = false\u65f6\uff0c\u6548\u679c\u4e0e\u4e0a\u9762\u7684\u7b80\u5355\u5b9e\u73b0\u6548\u679c\u76f8\u540c \r\n*\/\r\n\r\n   var context, args, result;\r\n    var timeout = null;\r\n    var previous = 0;\r\n    if (!options) options = {};\r\n    var later = function() {\r\n      previous = options.leading === false ? 0 : _.now();\r\n      timeout = null;\r\n      result = func.apply(context, args);\r\n      if (!timeout) context = args = null;\r\n    };\r\n    return function() {\r\n      var now = _.now();\r\n      if (!previous &amp;&amp; options.leading === false) previous = now;\r\n      \/\/ \u8ba1\u7b97\u5269\u4f59\u65f6\u95f4\r\n      var remaining = wait - (now - previous);\r\n      context = this;\r\n      args = arguments;\r\n      \/\/ \u5f53\u5230\u8fbewait\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\uff0c\u5219\u8c03\u7528func\u51fd\u6570\r\n      \/\/ \u7cbe\u5f69\u4e4b\u5904\uff1a\u6309\u7406\u6765\u8bf4remaining &lt;= 0\u5df2\u7ecf\u8db3\u591f\u8bc1\u660e\u5df2\u7ecf\u5230\u8fbewait\u7684\u65f6\u95f4\u95f4\u9694\uff0c\u4f46\u8fd9\u91cc\u8fd8\u8003\u8651\u5230\u5047\u5982\u5ba2\u6237\u7aef\u4fee\u6539\u4e86\u7cfb\u7edf\u65f6\u95f4\u5219\u9a6c\u4e0a\u6267\u884cfunc\u51fd\u6570\u3002\r\n      if (remaining &lt;= 0 || remaining &gt; wait) {\r\n        \/\/ \u7531\u4e8esetTimeout\u5b58\u5728\u6700\u5c0f\u65f6\u95f4\u7cbe\u5ea6\u95ee\u9898\uff0c\u56e0\u6b64\u4f1a\u5b58\u5728\u5230\u8fbewait\u7684\u65f6\u95f4\u95f4\u9694\uff0c\u4f46\u4e4b\u524d\u8bbe\u7f6e\u7684setTimeout\u64cd\u4f5c\u8fd8\u6ca1\u88ab\u6267\u884c\uff0c\u56e0\u6b64\u4e3a\u4fdd\u9669\u8d77\u89c1\uff0c\u8fd9\u91cc\u5148\u6e05\u7406setTimeout\u64cd\u4f5c\r\n        if (timeout) {\r\n          clearTimeout(timeout);\r\n          timeout = null;\r\n        }\r\n        previous = now;\r\n        result = func.apply(context, args);\r\n        if (!timeout) context = args = null;\r\n      } else if (!timeout &amp;&amp; options.trailing !== false) {\r\n        \/\/ options.trailing=true\u65f6\uff0c\u5ef6\u65f6\u6267\u884cfunc\u51fd\u6570\r\n        timeout = setTimeout(later, remaining);\r\n      }\r\n      return result;\r\n    };\r\n  };\r\n\r\n<\/pre>\n<p><strong>throttle \u5e94\u7528\u573a\u666f<\/strong><\/p>\n<ul>\n<li>DOM \u5143\u7d20\u7684\u62d6\u62fd\u529f\u80fd\u5b9e\u73b0\uff08mousemove\uff09<\/li>\n<li>\u5c04\u51fb\u6e38\u620f\u7684 mousedown\/keydown \u4e8b\u4ef6\uff08\u5355\u4f4d\u65f6\u95f4\u53ea\u80fd\u53d1\u5c04\u4e00\u9897\u5b50\u5f39\uff09<\/li>\n<li>\u8ba1\u7b97\u9f20\u6807\u79fb\u52a8\u7684\u8ddd\u79bb\uff08mousemove\uff09<\/li>\n<li>Canvas \u6a21\u62df\u753b\u677f\u529f\u80fd\uff08mousemove\uff09<\/li>\n<li>\u641c\u7d22\u8054\u60f3\uff08keyup\uff09<\/li>\n<li>\u76d1\u542c\u6eda\u52a8\u4e8b\u4ef6\u5224\u65ad\u662f\u5426\u5230\u9875\u9762\u5e95\u90e8\u81ea\u52a8\u52a0\u8f7d\u66f4\u591a\uff1a\u7ed9 scroll \u52a0\u4e86 debounce \u540e\uff0c\u53ea\u6709\u7528\u6237\u505c\u6b62\u6eda\u52a8\u540e\uff0c\u624d\u4f1a\u5224\u65ad\u662f\u5426\u5230\u4e86\u9875\u9762\u5e95\u90e8\uff1b\u5982\u679c\u662f throttle \u7684\u8bdd\uff0c\u53ea\u8981\u9875\u9762\u6eda\u52a8\u5c31\u4f1a\u95f4\u9694\u4e00\u6bb5\u65f6\u95f4\u5224\u65ad\u4e00\u6b21<\/li>\n<\/ul>\n<p><strong>debounce \u5e94\u7528\u573a\u666f<strong><br \/>\n\u51fd\u6570\u53bb\u6296\u6709\u54ea\u4e9b\u5e94\u7528\u573a\u666f\uff1f\u54ea\u4e9b\u65f6\u5019\u5bf9\u4e8e\u8fde\u7eed\u7684\u4e8b\u4ef6\u54cd\u5e94\u6211\u4eec\u53ea\u9700\u8981\u6267\u884c\u4e00\u6b21\u56de\u8c03\uff1f<\/strong><\/strong><\/p>\n<ul>\n<li>\u6bcf\u6b21 resize\/scroll \u89e6\u53d1\u7edf\u8ba1\u4e8b\u4ef6<\/li>\n<li>\u6587\u672c\u8f93\u5165\u7684\u9a8c\u8bc1\uff08\u8fde\u7eed\u8f93\u5165\u6587\u5b57\u540e\u53d1\u9001 AJAX \u8bf7\u6c42\u8fdb\u884c\u9a8c\u8bc1\uff0c\u9a8c\u8bc1\u4e00\u6b21\u5c31\u597d\uff09<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9879\u76ee\u4f18\u5316\u79cd\u53d1\u73b0\u5f88\u591a\u65f6\u5019\u9700\u8981\u8f93\u5165\u4e4b\u540e\u9a8c\u8bc1\uff0c\u56e0\u6b64\u7528\u5f97\u9891\u7387\u6bd4\u8f83\u9ad8\u76842\u4e2a\u51fd\u6570debounce\u548cthrottle\u3002\u8fd8\u6709\u4ee5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/yusemo.com\/index.php?rest_route=\/wp\/v2\/posts\/267"}],"collection":[{"href":"https:\/\/yusemo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yusemo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yusemo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yusemo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=267"}],"version-history":[{"count":3,"href":"https:\/\/yusemo.com\/index.php?rest_route=\/wp\/v2\/posts\/267\/revisions"}],"predecessor-version":[{"id":287,"href":"https:\/\/yusemo.com\/index.php?rest_route=\/wp\/v2\/posts\/267\/revisions\/287"}],"wp:attachment":[{"href":"https:\/\/yusemo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yusemo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yusemo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}