Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// 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
You can’t perform that action at this time.