Guest User

Untitled

a guest
Jan 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. while(that.funcns.length>0){
  2. fn=that.funcns.splice(0,1)[0];
  3. fn.idx=i;
  4. window.setTimeout(function(){
  5. fn.ref(); //call function reference
  6. },(idx==0)?idx:1000);
  7. }
  8. //fn - {ref:functionReference,order:true/false};
  9.  
  10. var funcs = [/* array of functions */];
  11.  
  12. function next() {
  13. var func = funcs.shift();
  14. if(func) {
  15. func();
  16. setTimeout(next, 100);
  17. }
  18. }
  19.  
  20. next();
  21.  
  22. var funcCount = 0, funcList = [];
  23.  
  24. function executeFunctions() {
  25. var nextFunc;
  26.  
  27. while (funcList.length > 0) {
  28. // Check next in list, if we need to wait, make sure we wait
  29. nextFunc = funcList[0];
  30. if (nextFunc.needToWait) {
  31. if (funcCount > 0) {
  32. // Try again later
  33. setTimeout(executeFunctions, 100);
  34. return;
  35. }
  36. }
  37.  
  38. // Since we are now going to execute, remove from list and execute
  39. funcList.splice(0, 1);
  40. funcCount += 1; // nextFunc will subtract 1 on completion
  41. setTimeout(nextFunc, 100);
  42. }
  43. }
  44.  
  45. // For async functions to call back to completion
  46. function completionCallback() {
  47. funcCount -= 1;
  48. }
  49.  
  50. // Example function 1 is simulated async
  51. function example1() {
  52. alert("Example1");
  53.  
  54. // Simulate async call with completion callback function, e.g. XHttpRequest
  55. setTimeout(completionCallback, 2000);
  56. }
  57. example1.needToWait = false; // Not flagged
  58.  
  59. // Example function is flagged as need others to complete first
  60. function example2() {
  61. alert("Example2");
  62.  
  63. funcCount -= 1;
  64. }
  65. example2.needToWait = true;
  66.  
  67. // Setup function list to execute example1 then example2
  68. funcList.push(example1);
  69. funcList.push(example2);
  70.  
  71. // OK, test it
  72. executeFunctions();
Add Comment
Please, Sign In to add comment