title: Determining where Maximum call stack size is being exceeded
body: <p>I have this function:</p>
  
  <pre><code>function _cssIsLoaded(cssStylesheet, stylePath) {
      var cssLoaded = 0;
      if (cssStylesheet.href == stylePath){
          try {
              if ( cssStylesheet.sheet &amp;&amp; cssStylesheet.sheet.cssRules.length &gt; 0 )
                  cssLoaded = 1;
              else if ( cssStylesheet.styleSheet &amp;&amp; cssStylesheet.styleSheet.cssText.length &gt; 0 )
                  cssLoaded = 1;
              else if ( cssStylesheet.innerHTML &amp;&amp; cssStylesheet.innerHTML.length &gt; 0 )
                  cssLoaded = 1;
              }
              catch(ex){ }
      }
          if(cssLoaded) {
              resetPops();
              $('#video-overlay').show();
              positionElements();
              saveBizzmail();
          } else {
              setTimeout(this._cssIsLoaded(cssStylesheet), 200);
          }
  </code></pre>
  
  <p>It's called by a stylesheet switching function. Occasionally, though I will get the error <code>Uncaught RangeError: Maximum call stack size exceeded,</code> when running the function. I presume this must be because it is looping constantly and the conditions are never being met?  I'm just not sure how to debug and have a look at the conditions when it is going to mess up, since the majority of times it works fine. Can I do the equivalent of setting a breakpoint in that function but only when the callstack exceeds a certain size?</p>
  
  <p>This is the function that switches stylesheets if that sheds some light:</p>
  
  <pre><code>function switchTemplate(stylePath){
  var osid = $('[id^="themeStyle-"]');
  var stylenum = osid[0].id.split('-')[1];
  var newstylenum = (Number(stylenum) + 1).toString();
  var ns = $('&lt;link&gt;', {
          href: stylePath,
          id: 'themeStyle-' + newstylenum,
          type: 'text/css', 
          rel: 'stylesheet'
      });
  $("head").append(ns);
  $('#themeStyle-' + stylenum).remove();
  _cssIsLoaded(ns.get(0), stylePath);
  }
  </code></pre>