Task_Array_Util

//https://stackoverflow.com/questions/15504921/asynchronous-loop-of-jquery-deferreds-promises?answertab=votes#tab-top

As of 2020-06-01. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/404464/811281/Task_Array_Util.js

  1. //slightly modified from
  2. //https://stackoverflow.com/questions/15504921/asynchronous-loop-of-jquery-deferreds-promises?answertab=votes#tab-top
  3.  
  4. /*
  5. TODO Description
  6. */
  7.  
  8. var time = Math.floor(Math.random() * 3000);
  9.  
  10. function doTask(taskNum, next, args) {
  11. log("doTask Enter");
  12. setTimeout(function () {
  13. log("TEST");
  14. taskNum(args);
  15. next();
  16. }, time)
  17. }
  18.  
  19. function createTask(taskNum, args){
  20. log("createTask Enter");
  21. return function(next){
  22. doTask(taskNum, next, args);
  23. }
  24. }
  25.  
  26. function queueTask(tasks, args) {
  27. log("queueTask Enter!");
  28. log("tasks | " + tasks);
  29. log("args | " + args);
  30. //tasks is an array of functions
  31. for (var i = 0; i < tasks.length; i++) {
  32. log("queueTask i " + i);
  33. $(document).queue('tasks', createTask(tasks[i], args[i]));
  34. }
  35.  
  36. $(document).queue('tasks', function () {
  37. log("All tasks dequeued");
  38. });
  39. dequeueTask();
  40. }
  41.  
  42. function dequeueTask() {
  43. log("dequeueTask Enter");
  44. $(document).dequeue('tasks');
  45. }