Created
April 27, 2018 05:29
-
-
Save cyan33/a52d323b1f58f1f854f613b914307e89 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){ | |
var id, timestamp; | |
var wait = 99; | |
var check = function(){ | |
var last = (Date.now()) - timestamp; | |
if (last < wait) { | |
id = setTimeout(check, wait - last); | |
} else { | |
id = null; | |
fn(); | |
} | |
}; | |
return function(){ | |
timestamp = Date.now(); | |
if(!id){ | |
id = setTimeout(check, wait); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment