git ssb

1+

Matt McKegg / mutant



Tree: 89e85f9009db15cde0d4a19d0f6fd5167a25d808

Files: 89e85f9009db15cde0d4a19d0f6fd5167a25d808 / once-idle.js

497 bytesRaw
1var queue = []
2var running = false
3var max = 1000 / 60
4
5module.exports = function (fn) {
6 if (typeof fn !== 'function') {
7 throw new Error('Must be a function')
8 }
9 queue.push(fn)
10 if (!running) {
11 running = true
12 window.requestIdleCallback(flush)
13 }
14}
15
16function flush () {
17 var startedAt = Date.now()
18
19 while (queue.length && Date.now() - startedAt < max) {
20 queue.shift()()
21 }
22
23 if (queue.length) {
24 window.requestIdleCallback(flush)
25 } else {
26 running = false
27 }
28}
29

Built with git-ssb-web