您好,欢迎访问一九零五行业门户网

函数节流与防抖的含义

function debounce(fn, delay, immediate){ var timeout, args, context, timestamp, result; var later = function(){ var last = date.now() - timestamp; if(last < delay && last >= 0){ timeout = settimeout(later, delay - last); }else{ timeout = null; if(!immediate){ result = fn.apply(context, args); if(!timeout){ context = args = null; } } } }; return function(){ context = this; args = arguments; timestamp = date.now(); console.log(timestamp); var callnow = immediate && !timeout; if(!timeout){ timeout = settimeout(later, delay); } if(callnow){ result = fn.apply(context, args); context = args = null; } return result } };function throttle(method , duration ,delay ){ var timer = null, // 记录下开始执行函数的时间 begin = new date(); return function(){ var context = this, args = arguments, // 记录下当前时间 current = new date(); // 函数节流里的思路 cleartimeout(timer); // 记录下的两个时间相减再与duration进行比较 if(current-begin >= duration){ method.apply(context , args); begin = current; }else{ timer = settimeout(function(){ method.apply(context , args); } , delay); } } } window.onresize = throttle(function(){console.log('resize')},1000,500)
以上就是函数节流与防抖的含义的详细内容。
其它类似信息

推荐信息