开发者

A "too much recursion" error in Firefox only sometimes?

开发者 https://www.devze.com 2023-03-24 02:39 出处:网络
I have a pretty simple thing I\'m doing with javascript and basically only sometimes will javascript give me a \"too much recursion\" error.

I have a pretty simple thing I'm doing with javascript and basically only sometimes will javascript give me a "too much recursion" error.

The code in question:

if(pageLoad===undefined){
  var pageLoad=function(){}; 
}
var pageLoad_uniqueid开发者_开发技巧_11=pageLoad;
var pageLoad=function(){ 
  pageLoad_uniqueid_11();
  pageLoad_uniqueid_12(); 
};
var pageLoad_uniqueid_12=function(){
 alert('pageLoad');
};

$(document).ready(function(){
   pageLoad();
});

(yes I know there are better way of doing this. This is difficult to change though, especially because of ASP.Net partial postbacks which aren't shown).

Anyway, when the too much recursion error happens, it continues to happen until I restart Firefox. when I restart Firefox it all works as normal again. How do I fix this?

I've also made a jsbin example

Update

Ok I've found out how to reliably reproduce it in our code, but it doesn't work for the jsbin example. If I create a new tab and go to the same page(have two tabs of the same address) and then refresh the first tab two times then I get this error consistently. We are not using any kind of session or anything else that I can think of that could cause such a problem to only occur in one tab!

Update 2 Not as reliable as I thought, but it definitely only occurs when more than one tab of the same page is open. It'll occur every few reloads of one of the tabs open

I've also updated my code to show an alert when pageLoad(the if statement) is initially undefined and when it is initially defined. Somehow, both alerts are showing up. This code is not duplicated in the rendered page and there is no way that it is being called twice. It is in a top level script element not surrounded by a function or anything! My code ends up looking like

if(pageLoad===undefined){ 
  var pageLoad=function(){}; 
  alert('new'); 
} else {  
  alert('old'); 
}


The code in question -- by itself -- should never result in an infinite recursion issue -- there is no function-statement and all the function objects are eagerly assigned to the variables. (If pageload is first undefined it will be assigned a No-Operation function, see next section.)

I suspect there is additional code/events that is triggering the behavior. One thing that may cause it is if the script/code is triggered twice during a page lifetime. The 2nd time pageload will not be undefined and will keep the original value, which if it is the function that calls the other two functions, will lead to infinite recursion.

I would recommend cleaning up the approach -- and having any issues caused by the complications just disappear ;-) What is the desired intent?

Happy coding.


This is just some additional info for other people trying to look for similar "too much recursion" errors in their code. Looks like firefox (as an example) gets too much recursion at about 6500 stack frames deep in this example: function moose(n){if(n%100 === 0)console.log(n);moose(n+1)};moose(0) . Similar examples can see depths of between 5000 and 7000. Not sure what the determining factors are, but it seems the number of parameters in the function drastically decrease the stack frame depth at which you get a "too much recursion" error. For example, this only gets to 3100:

function moose(n,m,a,s,d,fg,g,q,w,r,t,y,u,i,d){if(n%100 === 0)console.log(n);moose(n+1)};moose(0)

If you want to get around this, you can use setTimeout to schedule iterations to continue from the scheduler (which resets the stack). This obviously only works if you don't need to return something from the call:

function recurse(n) {
  if(n%100 === 0)
    setTimeout(function() {
      recurse(n+1)
    },0)
  else
    recurse(n+1)
}

Proper tail calls in ECMAScript 6 will solve the problem for some cases where you do need to return something from calls like this. Until then, for cases with deep recursion, the only answers are using either iteration, or the setTimeout method I mentioned.


I came across this error. The scenario in my case was different. The culprit code was something like this (which is simple concatenation recessively)

while(row)
{
    string_a .= row['name'];
}

I found that JavaScript throws error on 180th recursion. Up till 179 loop, the code runs fine.

The behaviors in Safaris is exactly the same, except that the error it shows is "RangeError: Maximum call stack size exceeded." It throws this error on 180 recursion as well.

Although this is not related to function call but it might help somebody who are stuck with it.


Afaik, this error can also appear if you state a wrong parameter for your ajax request, like

$.getJSON('get.php',{cmd:"1", elem:$('#elem')},function(data) { // ... }

Which then should be

elem:$('#elem').val()

instead.


This will also cause the "too much recursion" issue:

class account {
    constructor() {
        this.balance = 0;      // <-- property: balance
    }

    set balance( amount ) {    // <-- set function is the same name as the property.
        this.balance = amount; // <-- AND property: balance (unintended recursion here)
    }
}

var acc = new account();

Using unique names is important.

Ok, so why is this happening?

In the set function it isn't actually setting the property to amount, instead it's calling the set function again because in the scope of the set function it is the same syntax for both setting the property AND calling the set function.

Because in that scope this is the same as account and (account OR this).balance = amount can both call the set function OR set the property.

The solution to this is to simply change the name of either the property or the set function in any way (and of course update the rest of the code accordingly).

0

精彩评论

暂无评论...
验证码 换一张
取 消