Created
September 14, 2017 10:28
-
-
Save screeny05/fa1fa353de7e974d410e42eeaf745d2d to your computer and use it in GitHub Desktop.
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
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