Skip to content

Instantly share code, notes, and snippets.

@jwalsh
Created April 29, 2013 20:34
Show Gist options
  • Save jwalsh/5484557 to your computer and use it in GitHub Desktop.
Save jwalsh/5484557 to your computer and use it in GitHub Desktop.
// Purpose: Prevent a tag request from taking more than .5s.
// Workflow for timeout state
// script msft-timeout.js:13
// 9 bk_monitor msft-timeout.js:21
// bk_timeout msft-timeout.js:29
// bkContinue
// Add a script request with an onload
window.bk_loaded = false;
window.bk_started = new Date();
function bkLoaded() {
console.log('bkLoaded');
// The interval / timeout clearing could be moved here
window.bk_loaded = true;
}
// This is the base implementation of the JS tag
console.log('script');
//
var script ='http://tags.bluekai.com/site/14445?ret=js';
// var script = 'http://wal.sh/poc/fb-perf/2s-sleep-js.php';
document.write("<scr"+"ipt onload=\"bkLoaded()\" src=\"" + script + "\"></script>");
// Monitor for the completion of bk_loaded
var bk_monitor = window.setInterval(function() {
console.log('bk_monitor');
if (bk_loaded) {
bkContinue();
}
}, 50)
// Stop monitoring and continue
var bk_timeout = window.setTimeout(function() {
console.log('bk_timeout');
// Set up default values or error notes
var bk_results = {
"campaigns": [
]
};
bkContinue();
}, 500)
// Assume bkContinue is functionality that has a synchronous
// dependency on the tag
var bkContinue = function() {
console.log('bkContinue ' + (new Date() - bk_started));
// Stop monitoring
clearInterval(bk_monitor);
// Remove the timeout monitoring if the load doesn't finish
clearTimeout(bk_timeout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment