Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- while(that.funcns.length>0){
- fn=that.funcns.splice(0,1)[0];
- fn.idx=i;
- window.setTimeout(function(){
- fn.ref(); //call function reference
- },(idx==0)?idx:1000);
- }
- //fn - {ref:functionReference,order:true/false};
- var funcs = [/* array of functions */];
- function next() {
- var func = funcs.shift();
- if(func) {
- func();
- setTimeout(next, 100);
- }
- }
- next();
- var funcCount = 0, funcList = [];
- function executeFunctions() {
- var nextFunc;
- while (funcList.length > 0) {
- // Check next in list, if we need to wait, make sure we wait
- nextFunc = funcList[0];
- if (nextFunc.needToWait) {
- if (funcCount > 0) {
- // Try again later
- setTimeout(executeFunctions, 100);
- return;
- }
- }
- // Since we are now going to execute, remove from list and execute
- funcList.splice(0, 1);
- funcCount += 1; // nextFunc will subtract 1 on completion
- setTimeout(nextFunc, 100);
- }
- }
- // For async functions to call back to completion
- function completionCallback() {
- funcCount -= 1;
- }
- // Example function 1 is simulated async
- function example1() {
- alert("Example1");
- // Simulate async call with completion callback function, e.g. XHttpRequest
- setTimeout(completionCallback, 2000);
- }
- example1.needToWait = false; // Not flagged
- // Example function is flagged as need others to complete first
- function example2() {
- alert("Example2");
- funcCount -= 1;
- }
- example2.needToWait = true;
- // Setup function list to execute example1 then example2
- funcList.push(example1);
- funcList.push(example2);
- // OK, test it
- executeFunctions();
Add Comment
Please, Sign In to add comment