Skip to content

Instantly share code, notes, and snippets.

@mrtnbroder
Created April 26, 2017 12:21
Show Gist options
  • Select an option

  • Save mrtnbroder/fcf8086750642ccd6fd21f2b5bab4781 to your computer and use it in GitHub Desktop.

Select an option

Save mrtnbroder/fcf8086750642ccd6fd21f2b5bab4781 to your computer and use it in GitHub Desktop.
HowTo requestIdleCallback
import R from 'ramda'
const runTask = R.curry((taskList, deadline) => {
while ((deadline.timeRemaining() > 0 || deadline.didTimeout) && taskList.length) {
const task = taskList.shift()
task()
}
if (taskList.length) {
requestIdleCallback(runTask(taskList), { timeout: 1000 })
}
})
const initTask = () => {
const taskList = []
return function enqueueTask(f) {
taskList.push(f)
requestIdleCallback(runTask(taskList), { timeout: 1000 })
}
}
// ....somewhere else in your app
const app = () => {
const taskQueue = initTask()
const task = () => {
// do stuff
}
taskQueue(task)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment