underscore 的 debounce 的实现

debounce_.debounce(function, wait, [immediate])  Creates>Pass true for>var lazyLayout = _.debounce(calculateLayout, 300); $(window).resize(lazyLayout);



_.debounce = function(func, wait, immediate) {
	var timeout;
	return function() {
		var context = this, args = arguments;
		var later = function() {
			timeout = null;
			if (!immediate) func.apply(context, args);
		};
		var callNow = immediate && !timeout;
		clearTimeout(timeout);
		timeout = setTimeout(later, wait);
		if (callNow) func.apply(context, args);
	};
};
 
 
参考:http://underscorejs.org/
http://davidwalsh.name/function-debounce

你可能感兴趣的:(underscore 的 debounce 的实现)