Created
June 4, 2016 15:51
-
-
Save poxrud/f17c63febdd40cb1582e5ef56a6fb298 to your computer and use it in GitHub Desktop.
JS debounce
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
function debounce(func, delay) { | |
var alreadyScheduled; | |
var lastTimer; | |
return function() { | |
if (alreadyScheduled) { | |
clearTimeout(lastTimer); | |
} | |
alreadyScheduled = true; | |
lastTimer = setTimeout(function() { | |
func(); | |
alreadyScheduled = false; | |
}, delay); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment