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

is there anyway to detect recursive method in javascriptjQuery? - Stack Overflow

programmeradmin1浏览0评论

I am working on small part of calculation code. I need to identify whenever recursive occur in javascript/jQuery and i need to terminate that recursive.

Is there any api to support this in javascript/jQuery?

I am working on small part of calculation code. I need to identify whenever recursive occur in javascript/jQuery and i need to terminate that recursive.

Is there any api to support this in javascript/jQuery?

Share Improve this question edited Feb 4, 2014 at 9:42 noseratio 61.8k36 gold badges223 silver badges500 bronze badges asked Feb 4, 2014 at 4:38 PyarePyare 6812 gold badges11 silver badges32 bronze badges 14
  • A hint: js is single threaded – zerkms Commented Feb 4, 2014 at 4:40
  • Do you mean to stop infinite recursion? – Salman Arshad Commented Feb 4, 2014 at 4:42
  • A browser will do that :-D – zerkms Commented Feb 4, 2014 at 4:48
  • @zerkms - But JavaScript can also be multi-thread (Workers) – Derek 朕會功夫 Commented Feb 4, 2014 at 4:49
  • @Derek 朕會功夫: it's just an attempt to be so. You cannot run an arbitrary code from it. And you don't have much control over it. – zerkms Commented Feb 4, 2014 at 4:51
 |  Show 9 more ments

2 Answers 2

Reset to default 5

You could implement your own recursive protection. There is nothing built into jQuery that would natively support preventing recursion.

function myFunc(arg) {
    // if this function already executing and this is recursive call
    // then just return (don't allow recursive call)
    if (myFunc.in) {
        return;
    }

    // set flag that we're in this function
    myFunc.in = true;

    // put your function's code here


    // clear flag that we're in this function
    myFunc.in = false;

}

myFunc.in = false;

You could also turn the boolean into a counter and allow recursion only up to a certain number of levels.

FYI, because JS is single threaded, this should only be an issue that might need protection if your function takes some sort of callback from code that isn't yours. If it's all your own code, then you should just make sure your own code won't cause this sort of problem.


Here's a little more foolproof version that protects the counter in a closure so it can't be manipulated outside the function:

 var myFunc = (function() {
     var inCntr = 0;
     return function(args) {
         // protect against recursion
         if (inCntr !== 0) {
             return;
         }
         ++inCntr;

         try {

             // put your function's code here

         } finally {
             --inCntr;
         }

     }
 })();

Note: this uses a try/finally block so even if your code or any code you call throws an exception the counter is still cleared (so it never gets stuck).

Another dodgy trick. If you use something like .bind(this) for recursion or if you use arrow function, it won't work.

boom();

function boom () {
  if(arguments.callee === arguments.callee.caller) {
    console.log('no recursion will happen');
    return;
  }
  boom();
}

Simple solution could be a flag in a parameter

boom2();

function boom2 (calledRecursively) {
  if(calledRecursively) {
    console.log('no recursion will happen');
    return;
  }
  boom2(true);
}

发布评论

评论列表(0)

  1. 暂无评论