Last active
April 27, 2017 01:05
-
-
Save davidrleonard/713b6e3bce39813fd1cdb8f7fed507aa to your computer and use it in GitHub Desktop.
This file contains 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 callFnOnListInFrames(scope, list, fn, args) { | |
return new Promise(function(resolve, reject) { | |
args = args || []; | |
// var start = performance.now(); | |
var workInd = 0; | |
var results = []; | |
var start; | |
var elapsed; | |
function _workInOneFrame(now) { | |
start = now; | |
elapsed = 0; | |
while (workInd < list.length && elapsed < 16) { | |
results.push(fn.apply(scope, args.length ? args.concat([list[workInd]]) : [list[workInd]])); | |
workInd++; | |
elapsed = performance.now() - start; | |
} | |
if (workInd === list.length) { | |
// We finished everything. Stop. | |
return resolve(results); | |
} | |
window.requestAnimationFrame(_workInOneFrame); | |
} | |
window.requestAnimationFrame(_workInOneFrame); | |
}); | |
} | |
function count(a) { | |
return `I counted ${a}`; | |
} | |
callFnOnListInFrames(null, ['1','2','3'], count, []).then((result) => //... ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment