Last active
October 11, 2017 08:05
-
-
Save rohit012/b71ef75a21de184058acf93e6f6db625 to your computer and use it in GitHub Desktop.
js debounce function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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