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

jquery - javascript: function call to itself - Stack Overflow

programmeradmin2浏览0评论

I suppose the following code:

jQuery("#mybutton").click(function(){

    //do something

});

How could I recall to this function "anonymous"?, I can not put a name to this function:

var xfun = function(){

    //do something

}

jQuery("#mybutton").click(xfun);

I can do something like this:

var working = false;
jQuery("#mybutton").click(function(){

  if (working){
    var _this = this;
    _this._eventType = e.type;
    setTimeout(function() { jQuery(_this).trigger(_this._eventType); }, 200);
    return false;
  }
  //do something

});

what I need is something like this:

var working = false;
jQuery("#mybutton").click(function(){

  if (working){
    setTimeout( this_function, 200);
    return false;
  }
  //do something

});

thanks.

EDIT:

Solution:

jQuery("#mybutton").click(function(){
  if (working){
    var fn = arguments.callee;
    var _this = this;
    setTimeout(function(){fn.call(_this);}, 200);
    return false;
  }
  //do something    
});

I suppose the following code:

jQuery("#mybutton").click(function(){

    //do something

});

How could I recall to this function "anonymous"?, I can not put a name to this function:

var xfun = function(){

    //do something

}

jQuery("#mybutton").click(xfun);

I can do something like this:

var working = false;
jQuery("#mybutton").click(function(){

  if (working){
    var _this = this;
    _this._eventType = e.type;
    setTimeout(function() { jQuery(_this).trigger(_this._eventType); }, 200);
    return false;
  }
  //do something

});

what I need is something like this:

var working = false;
jQuery("#mybutton").click(function(){

  if (working){
    setTimeout( this_function, 200);
    return false;
  }
  //do something

});

thanks.

EDIT:

Solution:

jQuery("#mybutton").click(function(){
  if (working){
    var fn = arguments.callee;
    var _this = this;
    setTimeout(function(){fn.call(_this);}, 200);
    return false;
  }
  //do something    
});
Share Improve this question edited Nov 25, 2009 at 11:40 andres descalzo asked Nov 24, 2009 at 17:54 andres descalzoandres descalzo 15k14 gold badges65 silver badges117 bronze badges 1
  • 3 @Oscar: the question is "how do I re-run an anonymous function from inside the anonymous function?" – Crescent Fresh Commented Nov 24, 2009 at 18:00
Add a comment  | 

2 Answers 2

Reset to default 20

You can indeed name your anonymous function:

jQuery("#mybutton").click(function doWork(){
  if (working){
    setTimeout(doWork, 200);
    return false;
  }
  //do something    
});

You can also use arguments.callee:

jQuery("#mybutton").click(function(){
  if (working){
    setTimeout(arguments.callee, 200);
    return false;
  }
  //do something    
});

I'd go with the former.

Can you explain why you can't name the function? You can use arguments.callee to get a reference to the current function, but that is deprecated and I'm not sure how much support it has among current browsers.

发布评论

评论列表(0)

  1. 暂无评论