Skip to content

Instantly share code, notes, and snippets.

@rohit012 rohit012/debounce.js
Last active Oct 11, 2017

Embed
What would you like to do?
js debounce function
var debounce = function(fn, time, immediate) {
var timeout;
return function() {
var that = this;
var args = arguments;
var later = function() {
if(!immediate){
timeout = null;
func.call(that, args);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, time);
if(callNow) {
func.call(that, args);
}
};
}
window.onresize = debounce(resize, 3000);
function resize(e) {
console.log('height', window.innerHeight);
console.log('width', window.innerWidth);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.