Guest User

Untitled

a guest
May 27th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. // Based on Nicholas C Zakas' function in High Performance Javascript
  2.  
  3. function batchProcess(items, process, callback) {
  4. // Create a copy of the original items array so that our side effects
  5. // (calling .shift()) don't pollute anything outside of this scope.
  6. var todo = items.slice(0);
  7.  
  8. setTimeout(function () {
  9. var start = +new Date, result = true;
  10.  
  11. // Process the items in batch for 50ms, or until the iteration is
  12. // canceled (by the process function returning false).
  13. while
  14. (result !== false
  15. && todo.length > 0
  16. && ((+new Date) - start < 50))
  17. {
  18. result = process(todo.shift());
  19. }
  20.  
  21. // When the 50ms is up, let the UI thread update by defering the rest of
  22. // the iteration in a setTimeout.
  23. if (todo.length > 0 && result !== false) {
  24. setTimeout(arguments.callee, 25);
  25. } else {
  26. callback(items);
  27. }
  28. }, 25);
  29. };
Add Comment
Please, Sign In to add comment