Last active
December 14, 2015 23:19
-
-
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...
This file contains hidden or 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
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