Created
August 25, 2017 07:28
-
-
Save Robin-front/a4a62de055c6f122b4dddee970aaa84e 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
| function createIdleTask(){ | |
| var task = [], | |
| timer = null; | |
| function runTask(deadline){ | |
| var len = task.length; | |
| while(deadline.timeRemaining() > 0 && len--) { | |
| task.shift()(); | |
| } | |
| if (len > 0){ | |
| timer = requestIdleCallback(runTask); | |
| } | |
| } | |
| return { | |
| addTask: function (handle){ | |
| task.push(handle); | |
| timer = requestIdleCallback(runTask); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment