$(window).on('scroll.namespace', throttle(function () { console.log('call') }, 200));
$(window).on('resize.namespace', debounce(function () { console.log('call') }, 100));
// no leading call, only trailing | |
function debounce(callback, timeout, _time) { | |
timeout = timeout || 100; | |
return function debounce() { | |
window.clearTimeout(_time); | |
_time = window.setTimeout(callback, timeout); | |
} | |
} |
// leading call if event occurs `wait` ms after calling `throttle` | |
function throttle(callback, wait, _time) { | |
wait = wait || 200; | |
_time = Date.now(); | |
return function throttle() { | |
if ((_time + wait - Date.now()) < 0) { | |
callback(); | |
_time = Date.now(); | |
} | |
} | |
} |