1

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);
  }
})();
2
  • they end up calling each other Example please Commented Nov 16, 2012 at 8:13
  • Why would you have two functions named check? Maybe setInterval is what you want? You can do a clearInterval inside to stop iteration... Commented Nov 16, 2012 at 8:14

3 Answers 3

6

Well, you could use arguments.callee to 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 check call another check function if you have one alongside it. Sadly, though, it will in IE8 and earlier because they quite incorrectly leak the name check to 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:

(function() {
    // A check function
    check();
    function check () {
      if (typeof foo !='undefined') {
        // do stuff
      } else {
        console.log("Failed to load, trying again");
        setTimeout(function(){ check(); }, 10);
      }
    }
})();
(function() {
    // A complete different one
    check();
    function check () {
      if (typeof foo !='undefined') {
        // do other stuff
      } else {
        console.log("Failed to load, trying again");
        setTimeout(function(){ check(); }, 10);
      }
    }
})();
Sign up to request clarification or add additional context in comments.

2 Comments

Interesting, this ended up solving the re-calling problem... which I think might be related to some black magic with Meteor, as I couldn't re-create it in a static page. I hope meteor gets some kind of a module loader soon!
@SkinnyGeek1010: Interesting! Glad that helped.
3

How about using an interval instead removing the need to call itself, as this will be done automatically until you clear the interval:

(function(){
   var interval = setInterval(function(){
      if(typeof foo != 'undefined'){
         clearInterval(interval);
         // do stuff
      }else{
         console.log("Failed to load, trying again");
      }
   },10)
})();

1 Comment

Shoot, I didn't see this until after I accepted the other one. This basically scopes the var interval and also keeps running repeatedly unless foo is defined. Thanks!!
0

Why not do something like this instead:

var inter;

function check() {
    if (typeof foo !== 'undefined') {
        clearInterval(inter);
        // do stuff
    } else {
        console.log("Failed to load, trying again");
    }
}
inter = setInterval(check, 10);

It's not a drop in replacement, but I think it's better style.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.