You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Returns a function, that, as long as it continues to be invoked, will not be triggered.// The function will be called after it stops being called for N milliseconds.// If `immediate` is passed, trigger the function on the leading edge, instead of the trailing._.debounce=function(func,wait,immediate){vartimeout,result;varlater=function(context,args){timeout=null;if(args)result=func.apply(context,args);};vardebounced=restArgs(function(args){// 如果已有定时器 则清除if(timeout)clearTimeout(timeout);if(immediate){varcallNow=!timeout;// 此处的回调函数later不会被执行进func的apply方法,因为没有args// immediate为真的情况下 仅在wait秒内,反复调用的第一次触发一个func// 之后在wait毫秒以内再次进入此处时,timeout已存在,later函数就用于重新记录新的定时器timeout=setTimeout(later,wait);// timeout为null 对func的第一次调用if(callNow)result=func.apply(this,args);}else{timeout=_.delay(later,wait,this,args);}returnresult;});debounced.cancel=function(){clearTimeout(timeout);timeout=null;};returndebounced;};
Why Throttle
简单的说,函数节流能使得连续的函数执行,变为固定时间段间断地执行。还是以 scroll 事件为例,如果不加以节流控制:轻轻滚动下窗口,控制台打印了 N 多个 hello world 字符串。如果 scroll 回调不是简单的打印字符串,而是涉及一些 DOM 操作,这样频繁的调用,低版本浏览器可能就会直接假死,我们希望回调可以间隔时间段触发。
// Returns a function, that, when invoked, will only be triggered at most once during a given window of time.// Normally, the throttled function will run as much as it can,// without ever going more than once per `wait` duration;// but if you'd like to disable the execution on the leading edge, pass `{leading: false}`.// To disable execution on the trailing edge, ditto._.throttle=function(func,wait,options){vartimeout,context,args,result;varprevious=0;if(!options)options={};varlater=function(){previous=options.leading===false ? 0 : _.now();timeout=null;result=func.apply(context,args);if(!timeout)context=args=null;};varthrottled=function(){varnow=_.now();// 首次触发时 若leading=false 则previous为当前时间戳// 目的是让remaining为wait毫秒,不会立即触发funcif(!previous&&options.leading===false)previous=now;varremaining=wait-(now-previous);context=this;args=arguments;// 如果remaining <= 0 或者 remaining > wait(表示客户端系统时间被调整过)时// 1.如果存在定时器,把定时器清除 + 重置id// 2.立即执行func,并将这次触发throttled方法的时间戳保存// 3.如果不存在定时器,把上下文 + 参数列表重置if(remaining<=0||remaining>wait){if(timeout){clearTimeout(timeout);timeout=null;}previous=now;result=func.apply(context,args);if(!timeout)context=args=null;}// 不存在定时器 且未指定 options.trailing = false// 1.则在remaining毫秒后执行laterelseif(!timeout&&options.trailing!==false){timeout=setTimeout(later,remaining);}returnresult;};throttled.cancel=function(){clearTimeout(timeout);previous=0;timeout=context=args=null;};returnthrottled;};
difference between throttling and debouncing
Throttlingenforcesamaximumnumberoftimesafunctioncanbecalledovertime.Asin"execute this function at most once every 100 milliseconds."Debouncingenforcesthatafunctionnotbecalledagainuntilacertainamountoftimehaspassedwithoutitbeingcalled.Asin"execute this function only if 100 milliseconds have passed without it being called."
Why Debounce
浏览器中某些计算和处理要比其他的昂贵的多。例如,DOM操作比起非DOM交互需要更多的内存和CPU时间。连续尝试进行过多的DOM相关操作可能会导致浏览器挂起,有时候甚至会崩溃。尤其在IE中使用onresize事件处理程序的时候容易发生,当调整浏览器大小的时候,该事件连续触发。在onresize事件处理程序内部如果尝试进行DOM操作,其高频率的更改可能会让浏览器崩溃。
How Debounce
underscore - debounce 源码分析
Why Throttle
简单的说,函数节流能使得连续的函数执行,变为固定时间段间断地执行。还是以 scroll 事件为例,如果不加以节流控制:轻轻滚动下窗口,控制台打印了 N 多个 hello world 字符串。如果 scroll 回调不是简单的打印字符串,而是涉及一些 DOM 操作,这样频繁的调用,低版本浏览器可能就会直接假死,我们希望回调可以间隔时间段触发。
How Throttle
underscore - throttle 源码分析
difference between throttling and debouncing
学习资料
The Difference Between Throttling and Debouncing
Debouncing and Throttling Explained Through Examples
JavaScript 函数节流和函数去抖应用场景辨析
underscore 函数去抖的实现
underscore 函数节流的实现
The text was updated successfully, but these errors were encountered: