Skip to content

Instantly share code, notes, and snippets.

@ghostcode
Created January 3, 2016 07:59
Show Gist options
  • Select an option

  • Save ghostcode/dc20e0a8f76b73ca51de to your computer and use it in GitHub Desktop.

Select an option

Save ghostcode/dc20e0a8f76b73ca51de to your computer and use it in GitHub Desktop.
节流函数对高频操作限制调用次数,以此提高性能
var throttle = {
    switch:false,
    timer:100,
    process:function(method,context){
        var self = this;
        if(self.switch){
            return;
        }
        self.switch = true;
        setTimeout(function(){
            self.switch = false;
            method.call(context);
        },self.timer);
    }
}
function throttle(method,context){
    clearTimeout(method.flag);
    method.flag = setTimeout(function(){
        method.call(context);
    },100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment