函数节流

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);

 

posted @ 2015-08-04 22:07  simple_ac  阅读(113)  评论(0编辑  收藏  举报