git ssb

1+

Matt McKegg / mutant



Tree: 01e9e1d516a0bcf09d486425ab7cba0f919f5a1a

Files: 01e9e1d516a0bcf09d486425ab7cba0f919f5a1a / 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