Created
January 15, 2016 16:37
-
-
Save joshdcomp/693aa34e536f536a7492 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
// _AK is the global namespace convention we use for Attck-related stuff | |
var _AK = { | |
// array of hashes containing globalObject and callbacks for consumption in the _strobe method | |
strobeQueue: [], | |
// flag for publically checking to see if we're currently strobing | |
isStrobing = 0, | |
// **Internal method, don't use directly. Use `strobe`.** Recursive function that every 10ms evaluates | |
// an array of hashes for whether or not a callback can run based on certain globalObject. | |
_strobe: function() { | |
var cleanup = []; | |
var timeout; | |
for (var i = 0; i < this.strobeQueue.length; i++) { | |
var item = this.strobeQueue[i]; | |
if (typeof window[item.GLOBALwillBeLoaded] !== 'undefined' || item.failsafe > 200) { | |
cleanup.push(i); | |
item.callback(); | |
} | |
else { | |
item.failsafe++; | |
} | |
} | |
//remove items from the queue that have met their globalObject | |
for (var i = 0; i < cleanup.length; i++) { | |
var j = cleanup[i]; | |
this.strobeQueue.splice(j, 1); | |
} | |
// Only continue to strobe if there's something in the queue. The `strobe` method | |
// will kick _strobe off again if we've stopped | |
if (this.strobeQueue.length > 1) { | |
this.isStrobing = setTimeout( | |
function() { | |
this._strobe(); | |
}.bind(this), | |
10 | |
); | |
} | |
else { | |
this.isStrobing = 0; | |
} | |
}, | |
strobe: function(GLOBALwillBeLoaded, callback) { | |
var item = { | |
'GLOBALwillBeLoaded': GLOBALwillBeLoaded, | |
'callback': callback, | |
'failsafe': 0, | |
'timeout': 0, | |
}; | |
this.strobeQueue.push(item); | |
//if _strobe isn't running, make it | |
if (!this.isStrobing) this._strobe(); | |
}, | |
}; | |
// USAGE | |
var _ak_docload = function() { | |
$(function(){ | |
}); | |
} | |
_AK.strobe($, _ak_docload); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment