debounce
:调用需要等待nms,如期间再次调用则重新计时,之后执行throttle
:调用后nms无法再次调用
debounce
var debounce = function(fun,time){
var last;
const ctx = this;
const args = arguments;
return (function(){
clearTimeout(last)
last=setTimeout(()=>{
fun.call(ctx,args)
},time)
})()
}
throttle
var throttle = function(fun,delay){
let now = 0;
const ctx = this;
const args = arguments;
if(Date.now()-now>delay){
now = Date.now()
fun.apply(ctx,args)
}
}