Created
January 29, 2015 11:14
-
-
Save danprince/d4331e50088acc0cdd65 to your computer and use it in GitHub Desktop.
Debounce
This file contains hidden or 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(fn, interval) { | |
var lastCall; | |
return function() { | |
var now = Date.now(); | |
if(!lastCall || now - lastCall > interval) { | |
fn.apply(this, arguments); | |
lastCall = now; | |
} | |
}; | |
} |
This file contains hidden or 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 hello() { | |
return console.log('hello world'); | |
} | |
setInterval(debounce(hello, 2000), 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment