Skip to content

Instantly share code, notes, and snippets.

@jason-s13r
Last active December 14, 2015 23:19
Show Gist options
  • Save jason-s13r/5165067 to your computer and use it in GitHub Desktop.
Save jason-s13r/5165067 to your computer and use it in GitHub Desktop.
I only just noticed how Google Analytics sets up gaq = gaq || [] and the ndoes gaq.push([], ...) and stuff for commands. So I wondered how I might be able to make a similar queue thing. Felt like being silly a bit too...
var j = j || [];
j.push('first');
j.push('second.1', 'second.2');
j = (function(q){
function J() {
var self = this;
self.queue = [];
var secretKey = ((Math.random()*10e16) + new Date().getTime()).toString(36);
self.pushEvent = document.createEvent('Event');
self.pushEvent.initEvent('J-push-' + secretKey, true, true);
document.addEventListener('J-push-' + secretKey, function(e){
console.log(self.queue.shift());
}, false);
};
J.prototype.push = function() {
var self = this,
args = [].slice.call(arguments);
for (var i in args) {
self.queue.push(args[i]);
document.dispatchEvent(self.pushEvent);
}
};
var j = new J();
if (!!q) {
while (q.length > 0) j.push(q.shift());
}
return j;
})(j);
j.push('third.1', 'third.2');
j.push('last');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment