Created
April 14, 2016 18:12
-
-
Save OwenMelbz/fd607a800130e8a4ad8dc07a39ff3f38 to your computer and use it in GitHub Desktop.
Helper function that fires when an object becomes avaiable
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
//Inspired by http://fokkezb.nl/2016/04/14/how-to-wait-for-a-javascript-variable-to-be-defined/ | |
//var console = document.getElementById('console'); | |
//console.innerText = 'not loaded'; | |
var whenDefined = function($context, $variable, $callback){ | |
if( $context[$variable] ){ | |
$callback(); | |
} else { | |
Object.defineProperty($context, $variable, { | |
configurable: true, | |
enumerable: true, | |
writeable: true, | |
get: function() { | |
return this['_' + $variable]; | |
}, | |
set: function(val) { | |
this['_' + $variable] = val; | |
$callback(); | |
} | |
}); | |
} | |
} | |
//whenDefined(window, 'intercomSettings', function() { | |
// console.innerText = 'loaded'; | |
//}); | |
//setTimeout(function(){ | |
// window.intercomSettings = {blar: 'blar'}; | |
//}, 3000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
defined1 is never printed because 2nd whenDefined override property (due to same context/variable) previously defined.