I'm trying to implement a queue in javascript just like Google does. So far this is my code, I'm not sure if I'm doing it correctly or if there is a better way of doing it. Perhaps there is a name for doing it this way.
Here is my code:
var google = google || {};
google.cmd = google.cmd || [];
google.cmd.push(function() { alert("test"); });
google.cmd.push(function() { alert("test2"); });
function executeCmd() {
for (k=0; k < google.cmd.length;k++){
google.cmd[k]();
}
google.cmd.length = 0;
}
google.cmd.push = function(e) {
Array.prototype.push.apply(this,arguments);
executeCmd();
}
executeCmd();
google.cmd.push(function() { alert("test3"); });