dio-v
8/8/2013 - 11:34 AM

Zorgt er voor dat een functie maar 1x binnen 'time' uitgevoerd kan worden. Voorkomt dubbele acties. bv. $('input').on('input keyup', throttl

Zorgt er voor dat een functie maar 1x binnen 'time' uitgevoerd kan worden. Voorkomt dubbele acties. bv. $('input').on('input keyup', throttle(function() { // ... }, 1000));

function throttle (func, wait) {
    return function() {
        var that = this,
            args = [].slice(arguments);

        clearTimeout(func._throttleTimeout);

        func._throttleTimeout = setTimeout(function() {
            func.apply(that, args);
        }, wait);
    };
}
	$(window).scroll(throttle(function(){
			doSomething();
	},500));