Created
November 27, 2021 16:18
-
-
Save EvgenyArtemov/af1d9a6b56c7213e1cadb31699deaa64 to your computer and use it in GitHub Desktop.
requestIdleCallback__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
//based on http://modernjavascript.blogspot.de/2013/08/building-better-debounce.html | |
var debounce = function(func) { | |
var timeout, timestamp; | |
var wait = 99; | |
var run = function(){ | |
timeout = null; | |
func(); | |
}; | |
var later = function() { | |
var last = Date.now() - timestamp; | |
if (last < wait) { | |
setTimeout(later, wait - last); | |
} else { | |
(requestIdleCallback || run)(run); | |
} | |
}; | |
return function() { | |
timestamp = Date.now(); | |
if (!timeout) { | |
timeout = setTimeout(later, wait); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment