您的位置:首页 > 其它

函数节流

2015-08-04 22:07 357 查看
http://www.alloyteam.com/2012/11/javascript-throttle/

1. 必须用context传递this,否则setTimeout中this为window对象

function fn(){}
var throttle=function(fn,delay)
{
var timer=null;
return function()
{
var context=this,args=arguments;
clearTimeout(timer);
timer=setTimeout(function()
{
fn.apply(context,args);
console.log(context);//input
console.log(this);//window
},delay);
};
};
var btn=document.getElementById('my-btn');
btn.addEventListener('click',throttle(fn,50),false);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: