{"id":231818,"date":"2021-12-20T10:14:09","date_gmt":"2021-12-20T02:14:09","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=231818"},"modified":"2021-12-13T09:14:38","modified_gmt":"2021-12-13T01:14:38","slug":"anti-shake-function-implementation","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/anti-shake-function-implementation.html","title":{"rendered":"\u9632\u6296\u51fd\u6570Debounce\u5b9e\u73b0"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5b9e\u73b0\u539f\u7406\u5c31\u662f\u5229\u7528\u5b9a\u65f6\u5668\uff0c\u51fd\u6570\u7b2c\u4e00\u6b21\u6267\u884c\u65f6\u8bbe\u5b9a\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u4e4b\u540e\u8c03\u7528\u65f6\u53d1\u73b0\u5df2\u7ecf\u8bbe\u5b9a\u8fc7\u5b9a\u65f6\u5668\u5c31\u6e05\u7a7a\u4e4b\u524d\u7684\u5b9a\u65f6\u5668\uff0c\u5e76\u91cd\u65b0\u8bbe\u5b9a\u4e00\u4e2a\u65b0\u7684\u5b9a\u65f6\u5668\uff0c\u5982\u679c\u5b58\u5728\u6ca1\u6709\u88ab\u6e05\u7a7a\u7684\u5b9a\u65f6\u5668\uff0c\u5f53\u5b9a\u65f6\u5668\u8ba1\u65f6\u7ed3\u675f\u540e\u89e6\u53d1\u51fd\u6570\u6267\u884c\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u624b\u5199\u4e00\u4e2a debounce<\/strong><\/div>\n

\u9632\u6296\u51fd\u6570 debounce \u6307\u7684\u662f\u67d0\u4e2a\u51fd\u6570\u5728\u67d0\u6bb5\u65f6\u95f4\u5185\uff0c\u65e0\u8bba\u89e6\u53d1\u4e86\u591a\u5c11\u6b21\u56de\u8c03\uff0c\u90fd\u53ea\u6267\u884c\u6700\u540e\u4e00\u6b21\u3002<\/p>\n

\r\n\/\/ fn \u662f\u9700\u8981\u9632\u6296\u5904\u7406\u7684\u51fd\u6570 \r\n\/\/ wait \u662f\u65f6\u95f4\u95f4\u9694 \r\nfunction debounce(fn, wait = 50) { \r\n    \/\/ \u901a\u8fc7\u95ed\u5305\u7f13\u5b58\u4e00\u4e2a\u5b9a\u65f6\u5668 id \r\n    let timer = null \r\n    \/\/ \u5c06 debounce \u5904\u7406\u7ed3\u679c\u5f53\u4f5c\u51fd\u6570\u8fd4\u56de \r\n    \/\/ \u89e6\u53d1\u4e8b\u4ef6\u56de\u8c03\u65f6\u6267\u884c\u8fd9\u4e2a\u8fd4\u56de\u51fd\u6570 \r\n    return function(...args) { \r\n        \/\/ this\u4fdd\u5b58\u7ed9context \r\n        const context = this \r\n       \/\/ \u5982\u679c\u5df2\u7ecf\u8bbe\u5b9a\u8fc7\u5b9a\u65f6\u5668\u5c31\u6e05\u7a7a\u4e0a\u4e00\u6b21\u7684\u5b9a\u65f6\u5668 \r\n        if (timer) clearTimeout(timer) \r\n       \r\n       \/\/ \u5f00\u59cb\u8bbe\u5b9a\u4e00\u4e2a\u65b0\u7684\u5b9a\u65f6\u5668\uff0c\u5b9a\u65f6\u5668\u7ed3\u675f\u540e\u6267\u884c\u4f20\u5165\u7684\u51fd\u6570 fn \r\n        timer = setTimeout(() => { \r\n            fn.apply(context, args) \r\n        }, wait) \r\n    } \r\n} \r\n \r\n\/\/ DEMO \r\n\/\/ \u6267\u884c debounce \u51fd\u6570\u8fd4\u56de\u65b0\u51fd\u6570 \r\nconst betterFn = debounce(() => console.log('fn \u9632\u6296\u6267\u884c\u4e86'), 1000) \r\n\/\/ \u505c\u6b62\u6ed1\u52a8 1 \u79d2\u540e\u6267\u884c\u51fd\u6570 () => console.log('fn \u9632\u6296\u6267\u884c\u4e86') \r\ndocument.addEventListener('scroll', betterFn) <\/pre>\n

\u4e0d\u8fc7 underscore \u4e2d\u7684 debounce \u8fd8\u6709\u7b2c\u4e09\u4e2a\u53c2\u6570\uff1aimmediate \u3002\u8fd9\u4e2a\u53c2\u6570\u662f\u505a\u4ec0\u4e48\u7528\u7684\u5462?<\/p>\n

\u4f20\u53c2 immediate \u4e3a true\uff0c debounce\u4f1a\u5728 wait \u65f6\u95f4\u95f4\u9694\u7684\u5f00\u59cb\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570 \u3002(\u6ce8\uff1a\u5e76\u4e14\u5728 wait \u7684\u65f6\u95f4\u4e4b\u5185\uff0c\u4e0d\u4f1a\u518d\u6b21\u8c03\u7528\u3002)\u5728\u7c7b\u4f3c\u4e0d\u5c0f\u5fc3\u70b9\u4e86\u63d0\u4ea4\u6309\u94ae\u4e24\u4e0b\u800c\u63d0\u4ea4\u4e86\u4e24\u6b21\u7684\u60c5\u51b5\u4e0b\u5f88\u6709\u7528\u3002<\/p>\n

\u628a true \u4f20\u9012\u7ed9 immediate \u53c2\u6570\uff0c\u4f1a\u8ba9 debounce \u5728 wait \u65f6\u95f4\u5f00\u59cb\u8ba1\u7b97\u4e4b\u524d\u5c31\u89e6\u53d1\u51fd\u6570(\u4e5f\u5c31\u662f\u6ca1\u6709\u4efb\u4f55\u5ef6\u65f6\u5c31\u89e6\u53d1\u51fd\u6570)\uff0c\u800c\u4e0d\u662f\u8fc7\u4e86 wait \u65f6\u95f4\u624d\u89e6\u53d1\u51fd\u6570\uff0c\u800c\u4e14\u5728 wait \u65f6\u95f4\u5185\u4e5f\u4e0d\u4f1a\u89e6\u53d1(\u76f8\u5f53\u4e8e\u628a fn \u7684\u6267\u884c\u9501\u4f4f)\u3002\u5982\u679c\u4e0d\u5c0f\u5fc3\u70b9\u4e86\u4e24\u6b21\u63d0\u4ea4\u6309\u94ae\uff0c\u7b2c\u4e8c\u6b21\u63d0\u4ea4\u5c31\u4f1a\u4e0d\u4f1a\u6267\u884c\u3002<\/p>\n

\u90a3\u6211\u4eec\u6839\u636e immediate \u7684\u503c\u6765\u51b3\u5b9a\u5982\u4f55\u6267\u884c fn \u3002\u5982\u679c\u662f immediate \u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u7acb\u5373\u6267\u884c fn \uff0c\u5e76\u5728 wait \u65f6\u95f4\u5185\u9501\u4f4f fn \u7684\u6267\u884c\uff0c wait \u65f6\u95f4\u4e4b\u540e\u518d\u89e6\u53d1\uff0c\u624d\u4f1a\u91cd\u65b0\u6267\u884c fn \uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n

\r\n\/\/ immediate \u8868\u793a\u7b2c\u4e00\u6b21\u662f\u5426\u7acb\u5373\u6267\u884c \r\nfunction debounce(fn, wait = 50, immediate) { \r\n    let timer = null \r\n    return function(...args) { \r\n        \/\/ this\u4fdd\u5b58\u7ed9context \r\n        const context = this \r\n        if (timer) clearTimeout(timer) \r\n       \r\n       \/\/ immediate \u4e3a true \u8868\u793a\u7b2c\u4e00\u6b21\u89e6\u53d1\u540e\u6267\u884c \r\n       \/\/ timer \u4e3a\u7a7a\u8868\u793a\u9996\u6b21\u89e6\u53d1 \r\n        if (immediate && !timer) { \r\n            fn.apply(context, args) \r\n        } \r\n        \r\n        timer = setTimeout(() => { \r\n            fn.apply(context, args) \r\n        }, wait) \r\n    } \r\n} \r\n \r\n\/\/ DEMO \r\n\/\/ \u6267\u884c debounce \u51fd\u6570\u8fd4\u56de\u65b0\u51fd\u6570 \r\nconst betterFn = debounce(() => console.log('fn \u9632\u6296\u6267\u884c\u4e86'), 1000, true) \r\n\/\/ \u7b2c\u4e00\u6b21\u89e6\u53d1 scroll \u6267\u884c\u4e00\u6b21 fn\uff0c\u540e\u7eed\u53ea\u6709\u5728\u505c\u6b62\u6ed1\u52a8 1 \u79d2\u540e\u624d\u6267\u884c\u51fd\u6570 fn \r\ndocument.addEventListener('scroll', betterFn)<\/pre>\n
underscore \u6e90\u7801\u89e3\u6790<\/strong><\/div>\n

\u770b\u5b8c\u4e86\u4e0a\u6587\u7684\u57fa\u672c\u7248\u4ee3\u7801\uff0c\u611f\u89c9\u8fd8\u662f\u6bd4\u8f83\u8f7b\u677e\u7684\uff0c\u73b0\u5728\u6765\u5b66\u4e60\u4e0b underscore \u662f\u5982\u4f55\u5b9e\u73b0 debounce \u51fd\u6570\u7684\uff0c\u5b66\u4e60\u4e00\u4e0b\u4f18\u79c0\u7684\u601d\u60f3\uff0c\u76f4\u63a5\u4e0a\u4ee3\u7801\u548c\u6ce8\u91ca\uff0c\u672c\u6e90\u7801\u89e3\u6790\u4f9d\u8d56\u4e8e underscore 1.9.1 \u7248\u672c\u5b9e\u73b0\u3002<\/p>\n

\r\n\/\/ \u6b64\u5904\u7684\u4e09\u4e2a\u53c2\u6570\u4e0a\u6587\u90fd\u6709\u89e3\u91ca \r\n_.debounce = function(func, wait, immediate) { \r\n  \/\/ timeout \u8868\u793a\u5b9a\u65f6\u5668 \r\n  \/\/ result \u8868\u793a func \u6267\u884c\u8fd4\u56de\u503c \r\n  var timeout, result; \r\n \r\n  \/\/ \u5b9a\u65f6\u5668\u8ba1\u65f6\u7ed3\u675f\u540e \r\n  \/\/ 1\u3001\u6e05\u7a7a\u8ba1\u65f6\u5668\uff0c\u4f7f\u4e4b\u4e0d\u5f71\u54cd\u4e0b\u6b21\u8fde\u7eed\u4e8b\u4ef6\u7684\u89e6\u53d1 \r\n  \/\/ 2\u3001\u89e6\u53d1\u6267\u884c func \r\n  var later = function(context, args) { \r\n    timeout = null; \r\n    \/\/ if (args) \u5224\u65ad\u662f\u4e3a\u4e86\u8fc7\u6ee4\u7acb\u5373\u89e6\u53d1\u7684 \r\n    \/\/ \u5173\u8054\u5728\u4e8e _.delay \u548c restArguments \r\n    if (args) result = func.apply(context, args); \r\n  }; \r\n \r\n  \/\/ \u5c06 debounce \u5904\u7406\u7ed3\u679c\u5f53\u4f5c\u51fd\u6570\u8fd4\u56de \r\n  var debounced = restArguments(function(args) { \r\n    if (timeout) clearTimeout(timeout); \r\n    if (immediate) { \r\n      \/\/ \u7b2c\u4e00\u6b21\u89e6\u53d1\u540e\u4f1a\u8bbe\u7f6e timeout\uff0c \r\n      \/\/ \u6839\u636e timeout \u662f\u5426\u4e3a\u7a7a\u53ef\u4ee5\u5224\u65ad\u662f\u5426\u662f\u9996\u6b21\u89e6\u53d1 \r\n      var callNow = !timeout; \r\n      timeout = setTimeout(later, wait); \r\n      if (callNow) result = func.apply(this, args); \r\n    } else { \r\n     \/\/ \u8bbe\u7f6e\u5b9a\u65f6\u5668 \r\n      timeout = _.delay(later, wait, this, args); \r\n    } \r\n \r\n    return result; \r\n  }); \r\n \r\n  \/\/ \u65b0\u589e \u624b\u52a8\u53d6\u6d88 \r\n  debounced.cancel = function() { \r\n    clearTimeout(timeout); \r\n    timeout = null; \r\n  }; \r\n \r\n  return debounced; \r\n}; \r\n \r\n\/\/ \u6839\u636e\u7ed9\u5b9a\u7684\u6beb\u79d2 wait \u5ef6\u8fdf\u6267\u884c\u51fd\u6570 func \r\n_.delay = restArguments(function(func, wait, args) { \r\n  return setTimeout(function() { \r\n    return func.apply(null, args); \r\n  }, wait); \r\n}); <\/pre>\n

\u76f8\u6bd4\u4e0a\u6587\u7684\u57fa\u672c\u7248\u5b9e\u73b0\uff0cunderscore \u591a\u4e86\u4ee5\u4e0b\u51e0\u70b9\u529f\u80fd\u3002<\/p>\n

    1\u3001\u51fd\u6570 func \u7684\u6267\u884c\u7ed3\u675f\u540e\u8fd4\u56de\u7ed3\u679c\u503c result<\/ol>\n
      2\u3001\u5b9a\u65f6\u5668\u8ba1\u65f6\u7ed3\u675f\u540e\u6e05\u9664 timeout \uff0c\u4f7f\u4e4b\u4e0d\u5f71\u54cd\u4e0b\u6b21\u8fde\u7eed\u4e8b\u4ef6\u7684\u89e6\u53d1<\/ol>\n
        3\u3001\u65b0\u589e\u4e86\u624b\u52a8\u53d6\u6d88\u529f\u80fd cancel<\/ol>\n
          4\u3001immediate \u4e3a true \u540e\u53ea\u4f1a\u5728\u7b2c\u4e00\u6b21\u89e6\u53d1\u65f6\u6267\u884c\uff0c\u9891\u7e41\u89e6\u53d1\u56de\u8c03\u7ed3\u675f\u540e\u4e0d\u4f1a\u518d\u6267\u884c<\/ol>\n","protected":false},"excerpt":{"rendered":"

          \u9632\u6296\u51fd\u6570 debounce \u6307\u7684\u662f\u67d0\u4e2a\u51fd\u6570\u5728\u67d0\u6bb5\u65f6\u95f4\u5185\uff0c\u65e0\u8bba\u89e6\u53d1\u4e86\u591a\u5c11\u6b21\u56de\u8c03\uff0c\u90fd\u53ea\u6267\u884c\u6700\u540e\u4e00\u6b21\u3002 \/\/ fn […]<\/p>\n","protected":false},"author":1329,"featured_media":231821,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-231818","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=231818"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231818\/revisions"}],"predecessor-version":[{"id":231822,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/231818\/revisions\/231822"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/231821"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=231818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=231818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=231818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}