最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I call an anonymous function from inside itself in JavaScript? - Stack Overflow

programmeradmin1浏览0评论

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);
  }
})();

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);
  }
})();
Share Improve this question asked Nov 16, 2012 at 8:09 SkinnyG33kSkinnyG33k 1,7414 gold badges24 silver badges30 bronze badges 2
  • they end up calling each other Example please – Alvin Wong 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... – beatgammit Commented Nov 16, 2012 at 8:14
Add a ment  | 

3 Answers 3

Reset to default 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 plete different one
    check();
    function check () {
      if (typeof foo !='undefined') {
        // do other stuff
      } else {
        console.log("Failed to load, trying again");
        setTimeout(function(){ check(); }, 10);
      }
    }
})();

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)
})();

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.

发布评论

评论列表(0)

  1. 暂无评论