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="点击"/>