Created
May 11, 2011 01:16
-
-
Save gtzilla/965731 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CallerQueue.js | |
// 7codes | |
// | |
// Created by gregory tomlinson on 2011-05-10. | |
// Copyright 2011 the public domain. All rights reserved. | |
// | |
/* | |
Dependency, jQuery 1.5.2+ | |
Use the 'deferred' promise returned by functions | |
Sequential Calls.. Asynchronous / synchronous... | |
Usage: | |
var queue = new CallerQueue(); | |
queue.append( function, ["arg1", "argg1"], 15000 ); // last in timeout in milliseconds, null, false, use 0 | |
queue.append( function, [], 500 ); // last in timeout in milliseconds, null, false, use 0 | |
queue.execute(); | |
*/ | |
function CallerQueue() { | |
var lst=[], pos=-1; | |
function call_method( item ) { | |
var token = item.method.apply(item, item.args ); | |
if(token && token.done) { | |
token.done(function() { | |
run(); | |
}); | |
} else { | |
run(); | |
} | |
} | |
function run() { | |
pos+=1; | |
if(pos >= lst.length) { | |
return; | |
} | |
if(lst[pos].timeout) { | |
setTimeout( function() { | |
call_method( lst[pos] ); | |
}, lst[pos].timeout); | |
} else { | |
call_method( lst[pos] ); | |
} | |
} | |
this.append=function( method, args, timer_ms ) { | |
lst.push({ | |
"method" : method, | |
"timeout" : timer_ms, | |
"args" : args | |
}); | |
} | |
this.execute=function() { | |
run(); | |
// tod, replace with | |
// this.execute=run; ? | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment