title: Javascript recursivity check
body: <p>Is there a built-in way to check if a javascript function has been called recursively (directly or indirectly)?</p>
  
  <p>By recursively, I mean that the function could be anywhere in the recursion chain (the function doesn't have to be the direct invoker). </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Obviously, there's no built-in way to achieve what I want to do.  I though the plan B would be easy to implement so I came up with this solution (similar to Paolo answer):</p>
  
  <pre><code>    function checkRecursion() {
  
          var f = arguments.callee.caller;
  
          var caller = f;
          while (caller) {
              caller = caller.caller;
              if (caller === f) {
                  return true;
              }
          }
  
          return false;
  
      };
  </code></pre>
  
  <p>This function is working perfectly if you try to search the first recursive level.</p>
  
  <pre><code>    function fA() {
          if (checkRecursion()) {
              alert("End of recursion");
          }
          else {
              fB();
          }
  
      }
  
      function fB() {
          fA();
      }
  
      fA();
  </code></pre>
  
  <p>However, if you need to perform the same check on a function executed after an other recursion, you'll end up into an infinite loop:</p>
  
  <pre><code>    var count = 0;
  
      function fA() {
          if (checkRecursion()) {
              //I should get here but I get stuck in the checkRecursion()
              alert("End of recursion");
          }
          else {
              fB();
          }
  
      }
  
      function fB() {
  
          if (count &gt; 2) {
              fA();
          } else
          {
              count++;
              fC();
          }
  
      }
  
      function fC() {
          fB();
      }
  
      fA();
  </code></pre>
  
  <p>For some reason, the fB caller is fC, and the fC caller is fB so I can't travel back to the fA function as a caller of fB.  This issue is way more complicated than I though it would be.</p>