Legendary

李洋的学习笔记


防抖&节流

<p>一、防抖(重读CD):</p> <pre><code>function debounce(fn, wait) { var timer = null; return function () { var context = this var args = arguments if (timer) { clearTimeout(timer); timer = null; } timer = setTimeout(function () { fn.apply(context, args) }, wait) } } var fn = function () { console.log('boom') } setInterval(debounce(fn,500),1000) // 第一次在1500ms后触发,之后每1000ms触发一次 setInterval(debounce(fn,2000),1000) // 不会触发一次(我把函数防抖看出技能读条,如果读条没完成就用技能,便会失败而且重新读条) //链接:https://juejin.im/post/5a35ed25f265da431d3cc1b1</code></pre> <p>二、节流(定时触发):</p> <pre><code>function throttle(fn, gapTime) { let _lastTime = null; return function () { let _nowTime = + new Date() if (_nowTime - _lastTime &gt; gapTime || !_lastTime) { fn(); _lastTime = _nowTime } } } let fn = ()=&gt;{ console.log('boom') } setInterval(throttle(fn,1000),10) 链接:https://juejin.im/post/5a35ed25f265da431d3cc1b1</code></pre>

页面列表

ITEM_HTML