Skip to content

Instantly share code, notes, and snippets.

@rohit012
Last active October 11, 2017 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rohit012/b71ef75a21de184058acf93e6f6db625 to your computer and use it in GitHub Desktop.
Save rohit012/b71ef75a21de184058acf93e6f6db625 to your computer and use it in GitHub Desktop.
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