Skip to content

Instantly share code, notes, and snippets.

@screeny05
Created September 14, 2017 10:28
Show Gist options
  • Save screeny05/fa1fa353de7e974d410e42eeaf745d2d to your computer and use it in GitHub Desktop.
Save screeny05/fa1fa353de7e974d410e42eeaf745d2d to your computer and use it in GitHub Desktop.
var idleThrottle = function(timeoutMin, timeoutMax, cb){
var idleId;
return function(){
var lastCall = Date.now();
var queueIdleCheck = function(timeout){
idleId = requestIdleCallback(function(){
var currentCall = Date.now();
var timeSinceLastCall = currentCall - lastCall;
if(timeSinceLastCall < timeoutMin){
console.log('not ready, requeue', timeSinceLastCall)
return queueIdleCheck(timeoutMax - timeSinceLastCall);
}
console.log('called in', timeSinceLastCall);
lastCall = currentCall;
return cb();
}, { timeout: timeout });
};
if(idleId){
cancelIdleCallback(idleId);
}
queueIdleCheck(timeoutMax);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment