{"id":1755,"date":"2020-07-24T14:51:21","date_gmt":"2020-07-24T06:51:21","guid":{"rendered":"https:\/\/www.yumefx.com\/?p=1755"},"modified":"2021-03-02T15:34:01","modified_gmt":"2021-03-02T07:34:01","slug":"js%e5%87%bd%e6%95%b0%e9%98%b2%e6%8a%96%e4%b8%8e%e5%87%bd%e6%95%b0%e8%8a%82%e6%b5%81","status":"publish","type":"post","link":"https:\/\/www.yumefx.com\/?p=1755","title":{"rendered":"JS\u51fd\u6570\u9632\u6296\u4e0e\u51fd\u6570\u8282\u6d41"},"content":{"rendered":"\n<p>\u6700\u8fd1\u5199\u4e86\u4e00\u4e9b\u524d\u7aef\u4ee3\u7801\uff0c\u611f\u89c9\u524d\u7aef\u4e5f\u633a\u6709\u8da3\u7684\uff0c\u56e0\u6b64\u4ee5\u540e\u4f1a\u4e0d\u5b9a\u65f6\u66f4\u65b0\u524d\u7aef\u7684\u535a\u6587\u3002<\/p>\n<p>\u56e0\u4e3a\u524d\u7aef\u8fd9\u5757\u6211\u4e5f\u662f\u73b0\u5b66\u73b0\u5356\uff0c\u56e0\u6b64\u4f9d\u7136\u91cd\u590d\u7740\u9700\u6c42-\u5b66\u4e60-\u5b9e\u73b0-\u53d1\u73b0\u95ee\u9898-\u89e3\u51b3\u95ee\u9898-\u63a2\u6c42\u539f\u7406\u7684\u8fc7\u7a0b\u3002<\/p>\n<p>\u00a0<\/p>\n<h1 style=\"text-align: center;\">\u95ee\u9898\u63cf\u8ff0<\/h1>\n<p>\u9700\u6c42\u4e2d\u6709\u4e00\u90e8\u5206\u662f\u4e00\u4e2a\u6ed1\u5757\u63a7\u5236\uff0c\u7528\u6237\u53ef\u4ee5\u6ed1\u52a8\u6ed1\u5757\u5411\u540e\u53f0\u63a5\u53e3\u4f20\u5165\u6570\u503c\uff0c\u540e\u53f0\u6839\u636e\u4f20\u5165\u6570\u503c\u5b9e\u73b0\u67d0\u4e9b\u529f\u80fd\u3002\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u6837\u3002<\/p>\n<p><img loading=\"lazy\" class=\"size-full wp-image-1756 aligncenter\" src=\"http:\/\/www.yumefx.com\/wp-content\/uploads\/2020\/07\/jsfangdou_01.jpg\" alt=\"\" width=\"650\" height=\"389\" \/><\/p>\n<p>\u6ed1\u5757\u90e8\u5206\u5b9e\u73b0\u5c31\u4e0d\u8be6\u7ec6\u8bf4\u4e86\uff0c\u5c31\u662f\u4f7f\u7528 <code>input type=\"range\"<\/code> \u5143\u7d20\u518d\u52a0\u4e0aCSS\u6837\u5f0f\u7f8e\u5316\u800c\u5df2\uff0c\u6ed1\u5757\u8ddf\u6570\u503c\u7684\u540c\u6b65\u5219\u662f\u901a\u8fc7js\u4ee3\u7801\u3002<\/p>\n<p>\u5728\u62d6\u52a8\u6ed1\u5757\u65f6\uff0c\u89e6\u53d1\u7684oninput\u4e8b\u4ef6\u4f1a\u901a\u8fc7js\u8c03\u7528\u540e\u53f0\u63a5\u53e3\uff0c\u8fd9\u91cc\u7684\u95ee\u9898\u662f\uff0c\u5728\u6ed1\u52a8\u6ed1\u5757\u65f6\uff0c\u89e6\u53d1\u9891\u7387\u592a\u9ad8\uff0c\u4f8b\u5982\u4ece10\u6ed1\u523020\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u5411\u540e\u53f0\u8fde\u7eed\u8c03\u752810\u6b21\u63a5\u53e3\uff0c\u4f20\u516511~20\u8fd910\u4e2a\u53c2\u6570\u3002<\/p>\n<p>\u8fd9\u663e\u7136\u662f\u6709\u95ee\u9898\u7684\uff0c\u7528\u6237\u53ea\u662f\u60f3\u5f97\u523020\u7684\u6548\u679c\uff0c\u5176\u4ed6\u6570\u503c\u662f\u4e0d\u5e94\u8be5\u4f20\u5165\u7684\uff0c\u8fd9\u4e9b\u591a\u4f59\u7684\u8f93\u5165\u4f1a\u8ba9\u540e\u53f0\u6d6a\u8d39\u5927\u91cf\u6027\u80fd\u3002<\/p>\n<p>\u56e0\u6b64\u8fd9\u91cc\u5f15\u5165\u4e86\u51fd\u6570\u9632\u6296\u548c\u51fd\u6570\u8282\u6d41\u7684\u6982\u5ff5\uff0c\u7528\u6765\u63a7\u5236\u51fd\u6570\u5728\u4e00\u5b9a\u65f6\u95f4\u5185\u7684\u6267\u884c\u6b21\u6570\uff0c\u4e0b\u9762\u5206\u522b\u4ecb\u7ecd\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">\u51fd\u6570\u9632\u6296<\/h1>\n<p>\u51fd\u6570\u9632\u6296\u7684\u6982\u5ff5\u662f\u521b\u5efa\u4e00\u4e2a\u8ba1\u65f6\u5668\uff0c\u51fd\u6570\u5c06\u5728\u8ba1\u65f6\u5b8c\u6bd5\u540e\u518d\u6267\u884c\uff08\u5ef6\u65f6\u6267\u884c\uff09\uff0c\u5982\u679c\u5728\u8ba1\u65f6\u5b8c\u6bd5\u4e4b\u524d\u4f20\u5165\u65b0\u7684\u540c\u4e00\u8bf7\u6c42\uff0c\u5219\u8ba1\u65f6\u5668\u6e05\u96f6\u91cd\u65b0\u7b49\u5f85\uff0c\u6700\u540e\u6267\u884c\u7684\u51fd\u6570\u4f20\u5165\u7684\u53c2\u6570\u662f\u6700\u540e\u4e00\u6b21\u4f20\u5165\u7684\u53c2\u6570\u3002<\/p>\n<p>\u51fd\u6570\u9632\u6296\u4e3b\u8981\u7528\u6765\u9632\u6b62\u8fde\u7eed\u8c03\u7528\uff0c\u4f8b\u5982\u8f93\u5165\u6846\u5185\u7684\u9a8c\u8bc1\u529f\u80fd\u3001\u6ed1\u5757\u7b49\u53c2\u6570\u8fde\u7eed\u4fee\u6539\u529f\u80fd\u3001\u9632\u6b62\u8868\u5355\u591a\u6b21\u63d0\u4ea4\u7b49\u7b49\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre>\n<span>function debounce(fn,wait) {<\/span>\n<span>    var timer = null;<\/span>\n<span>    return function () {<\/span>\n<span>        var context = this;<\/span>\n<span>        var args = arguments;<\/span>\n<span>        if (timer) {<\/span>\n<span>            clearTimeout(timer);<\/span>\n<span>            timer = null;<\/span>\n<span>        }<\/span>\n<span>        timer = setTimeout(function () {<\/span>\n<span>            fn.apply(context,args)<\/span>\n<span>        },wait)<\/span>\n<span>    }<\/span>\n<span>}<\/span>\n<span>var fn = function () {<\/span>\n<span>    console.log(&#x27;boom&#x27;)<\/span>\n<span>}<\/span>\n<span>setInterval(debounce(fn,500),1000) \/\/\u7b2c\u4e00\u6b21\u57281500ms\u540e\u89e6\u53d1\uff0c\u4e4b\u540e\u6bcf1000ms\u89e6\u53d1\u4e00\u6b21<\/span>\n<span>setInterval(debounce(fn,2000),1000) \/\/\u4e0d\u4f1a\u89e6\u53d1\uff0c\u56e0\u4e3a\u4e00\u76f4\u5728\u91cd\u7f6e\u8ba1\u65f6\u5668<\/span>\n<\/pre>\n\n\n\n<p>\u5176\u4e2d\u5bf9<code>timer<\/code>\u53d8\u91cf\u505a\u4e86\u4e00\u6b21\u95ed\u5305\u3002<\/p>\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">\u51fd\u6570\u8282\u6d41<\/h1>\n<p>\u51fd\u6570\u8282\u6d41\u7684\u6982\u5ff5\u662f\u89c4\u5b9a\u4e00\u4e2a\u5355\u4f4d\u65f6\u95f4\uff0c\u5728\u5355\u4f4d\u65f6\u95f4\u5185\u53ea\u80fd\u89e6\u53d1\u4e00\u6b21\u51fd\u6570\u6267\u884c\uff0c\u5176\u4ed6\u6267\u884c\u4f1a\u88ab\u5ffd\u7565\u3002<\/p>\n<p>\u51fd\u6570\u8282\u6d41\u4e3b\u8981\u7528\u4e8e\u9501\u5b9a\u8c03\u7528\u9891\u7387\uff0c\u4f8b\u5982\u6e38\u620f\u5237\u65b0\u7387\u3001DOM\u5143\u7d20\u62d6\u62fd\u3001Canvas\u753b\u7b14\u529f\u80fd\u7b49\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre>\n<span>function throttle(fn,gapTime) {<\/span>\n<span>    let _lastTime = null;<\/span>\n<span>    return function () {<\/span>\n<span>        let _nowTime = + new Date();<\/span>\n<span>        if (_nowTime - _lastTime &gt; gapTime || !_lastTime) {<\/span>\n<span>            fn();<\/span>\n<span>            _lastTime = _nowTime;<\/span>\n<span>        }<\/span>\n<span>    }<\/span>\n<span>}<\/span>\n<span>let fn = ()=&gt;{<\/span>\n<span>    console.log(&#x27;boom&#x27;)<\/span>\n<span>}<\/span>\n<span>setInterval(throttle(fn,1000),10) \/\/\u6bcf\u79d2\u89e6\u53d1\u4e00\u6b21<\/span>\n<\/pre>\n\n\n\n<p>\u5176\u4e2d\u5bf9<code>_lastTime<\/code>\u53d8\u91cf\u505a\u4e86\u4e00\u6b21\u95ed\u5305\u3002<\/p>\n<p>\u4e0d\u8fc7\u8fd9\u4e48\u5199\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u5373\u6700\u540e\u4e00\u6b21\u89e6\u53d1\u5f88\u53ef\u80fd\u65e0\u6cd5\u6267\u884c\u3002<\/p>\n<p>\u56e0\u6b64\u53ef\u4ee5\u7ed3\u5408\u51fd\u6570\u9632\u6296\u5b9e\u73b0\u5982\u4e0b<\/p>\n\n\n\n<pre>\n<span>function throttle2(method,delay,time) {<\/span>\n<span>    var timeout,startTime = new Date();<\/span>\n<span>    return function() {<\/span>\n<span>        var context = this;<\/span>\n<span>        var args = arguments;<\/span>\n<span>        var curTime = new Date();<\/span>\n<span>        clearTimeout(timeout);<\/span>\n<span>        \/\/\u8fbe\u5230\u89e6\u53d1\u95f4\u9694\u5219\u7acb\u5373\u89e6\u53d1<\/span>\n<span>        if (curTime - startTime &gt;= time) {<\/span>\n<span>            method.apply(context,args);<\/span>\n<span>            startTime = curTime;<\/span>\n<span>        \/\/\u6700\u540e\u4e00\u6b21\u5ef6\u65f6\u89e6\u53d1<\/span>\n<span>        } else {<\/span>\n<span>            timeout = setTimeout(method,delay);<\/span>\n<span>        }<\/span>\n<span>    }<\/span>\n<span>}<\/span>\n<\/pre>\n\n\n\n<p>\u6216\u8005<\/p>\n\n\n\n<pre>\n<span>var lastTime = null;<\/span>\n<span>var timeout = null;<\/span>\n<span>function throttle(fn\uff0cdelay) {<\/span>\n<span>    let nowTime = new Date();<\/span>\n<span>    clearTimeout(timeout);<\/span>\n<span>    if (nowTime - lastTime &gt; delay || !lastTime) {<\/span>\n<span>        fn();<\/span>\n<span>        lastTime = nowTime;<\/span>\n<span>    }else{<\/span>\n<span>        timeout = setTimeout(fn,delay);<\/span>\n<span>    }<\/span>\n<span>}<\/span>\n<\/pre>\n\n\n\n<h1>\u00a0<\/h1>\n<h1 style=\"text-align: center;\">\u603b\u7ed3<\/h1>\n<p>\u5728\u9047\u5230\u51fd\u6570\u89e6\u53d1\u6b21\u6570\u592a\u9891\u7e41\u7684\u95ee\u9898\u540e\uff0c\u901a\u8fc7\u67e5\u8d44\u6599\u627e\u5230\u4e86\u8fd9\u4e24\u79cd\u65b9\u6cd5\uff0c\u8fd9\u91cc\u660e\u663e\u5e94\u8be5\u4f7f\u7528\u51fd\u6570\u9632\u6296\uff0c\u6210\u529f\u89e3\u51b3\u4e86\u8be5\u95ee\u9898\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u5728\u8fd9\u4e2a\u6848\u4f8b\u91cc\u4e5f\u53ef\u4ee5\u4f7f\u7528onchange\u4e8b\u4ef6\uff0c\u5728\u677e\u5f00\u9f20\u6807\u7684\u65f6\u5019\u624d\u4f1a\u89e6\u53d1\uff0c\u6570\u503c\u663e\u793a\u5219\u4f7f\u7528oninput\u5b9e\u65f6\u663e\u793a\u5f53\u524d\u6570\u503c\u3002<\/p>\n<p>\u672c\u6587\u4ee3\u7801\u90e8\u5206\u8f6c\u8f7d\u81ea<a href=\"https:\/\/mp.weixin.qq.com\/s\/3FZJ0nQLhj9PCi0pfBjc9A\">\u8f7b\u677e\u7406\u89e3JS\u51fd\u6570\u8282\u6d41\u548c\u51fd\u6570\u9632\u6296<\/a>\u3002<\/p>\n<p>\u00a0<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u6240\u8c13\u81ea\u5f8b\uff0c\u662f\u4ee5\u79ef\u6781\u800c\u4e3b\u52a8\u7684\u6001\u5ea6\uff0c<\/p>\n<p>\u53bb\u89e3\u51b3\u4eba\u751f\u75db\u82e6\u7684\u91cd\u8981\u539f\u5219\uff0c<\/p>\n<p>\u4e3b\u8981\u5305\u62ec\u56db\u4e2a\u65b9\u9762\uff1a<\/p>\n<p>\u63a8\u8fdf\u6ee1\u8db3\u611f\u3001\u627f\u62c5\u8d23\u4efb\u3001\u5c0a\u91cd\u4e8b\u5b9e\u3001\u4fdd\u6301\u5e73\u8861\u3002<\/p>\n<p style=\"text-align: right;\">\u300a\u5c11\u6709\u4eba\u8d70\u7684\u8def\u300b<\/p>\n<p style=\"text-align: right;\">\u2014\u2014M\u00b7\u65af\u79d1\u7279\u00b7\u6d3e\u514b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5199\u4e86\u4e00\u4e9b\u524d\u7aef\u4ee3\u7801\uff0c\u611f\u89c9\u524d\u7aef\u4e5f\u633a\u6709\u8da3\u7684\uff0c\u56e0\u6b64\u4ee5\u540e\u4f1a\u4e0d\u5b9a\u65f6\u66f4\u65b0\u524d\u7aef\u7684\u535a\u6587\u3002 \u56e0\u4e3a\u524d\u7aef\u8fd9\u5757\u6211\u4e5f\u662f\u73b0\u5b66\u73b0\u5356\uff0c\u56e0\u6b64\u4f9d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1758,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18,4],"tags":[42,34,15],"_links":{"self":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts\/1755"}],"collection":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1755"}],"version-history":[{"count":0,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/posts\/1755\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=\/wp\/v2\/media\/1758"}],"wp:attachment":[{"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yumefx.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}