leafiy
5/18/2018 - 6:46 PM

函数节流 throttle #js

none

var processor = {
    	timeoutId:null,
    	performProcessoring:function(){
    		// do something
    	},
    	process:function(){
    		clearTimeout(this.timeoutId);
    		var that = this;
    		this.timeoutId = setTimeout(function(){
    			that.performProcessoring();
    		},100)
    	}
}
/** throttle**/
function throttle(method,context){
    	clearTimeout(method.tId);
    	method.tId = setTimeout(function(){
    		method.call(context);
    	},100)
}

//example
function resizeDiv(){
    
    
    }
    
    window.onresize = function(){
        throttle(resizeDiv)
    }