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/811172/Task_Array_Util.js

  1. // ==UserScript==
  2. // @name Task_Array_Util
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description //https://stackoverflow.com/questions/15504921/asynchronous-loop-of-jquery-deferreds-promises?answertab=votes#tab-top
  6. // @author Dylan Banta
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. /*
  11. TODO Description
  12. */
  13.  
  14. function doTask(taskNum) {
  15. log("doTask Enter");
  16. var time = Math.floor(Math.random() * 3000);
  17.  
  18. setTimeout(function () {
  19. console.log(taskNum);
  20. dequeTask();
  21. }, time)
  22. }
  23.  
  24. function createTask(taskNum) {
  25. log("createTask Enter");
  26. return function () {
  27. doTask(taskNum);
  28. }
  29. }
  30.  
  31. function queueTask(tasks) {
  32. log("queTask Enter");
  33. //Loops through task array
  34. for (var i in tasks) {
  35. $(document).queue('tasks', function(){
  36. createTask(tasks[i]);
  37. });
  38. }
  39.  
  40. //Add a logger to the end of the task stating that all tasks have been completed
  41. $(document).queue('tasks', function () {
  42. log("All tasks completed");
  43. });
  44. }
  45.  
  46. function dequeueTask() {
  47. $(document).dequeue('tasks');
  48. }