js 函数节流

function throttle(fn,delay){
var timer = null;
return function (){//使用闭包,保证了全局的timer
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function (){
fn.apply(context,args);//context保证了fn里的thi
},delay);
};
};


function fn(){
console.log("执行");
}

$("#btn").on("click",throttle(fn, 500));
<input id="btn" type="button"  value="点击"/>
posted @ 2016-09-09 10:50  G善源  阅读(80)  评论(0编辑  收藏  举报