Is it possible to re-run the function ‘check’ without calling check() in the else statement? Maybe call itself somehow?
If I have multiple instances of this function, they end up calling each other, unless I change it to check1, check2, etc.. While this is prob. a performance killer, I was just trying it out for a prototype app.
(function check () {
if (typeof foo !='undefined') {
// do stuff
} else {
console.log("Failed to load, trying again");
setTimeout(function(){ check(); }, 10);
}
})();
Well, you could use
arguments.calleeto call it, but it’s a bad idea, a performance hit, and it isn’t valid in strict mode.What you’ve quoted is valid JavaScript and should not make
checkcall anothercheckfunction if you have one alongside it. Sadly, though, it will in IE8 and earlier because they quite incorrectly leak the namecheckto the surrounding scope (named function expressions do not add the function name to the surrounding scope like function declarations do — except on IE8 and earlier [and that’s not all they get wrong with them]).So the solution is to use an anonymous function to hide a named one: