javascript: function call to itself

javascript, jquery

Solution

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.

Problem

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

Original source