Skip to content

Instantly share code, notes, and snippets.

@EvgenyArtemov
Created November 27, 2021 16:18
Show Gist options
  • Save EvgenyArtemov/af1d9a6b56c7213e1cadb31699deaa64 to your computer and use it in GitHub Desktop.
Save EvgenyArtemov/af1d9a6b56c7213e1cadb31699deaa64 to your computer and use it in GitHub Desktop.
requestIdleCallback__debounce
//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