Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
37
rated 0 times [  38] [ 1]  / answers: 1 / hits: 79308  / 13 Years ago, fri, july 29, 2011, 12:00:00

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

More From » firefox

 Answers
14

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.


[#90916] Thursday, July 28, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaylynbrynnk

Total Points: 706
Total Questions: 98
Total Answers: 91

Location: Israel
Member since Thu, Jan 7, 2021
3 Years ago
jaylynbrynnk questions
Thu, Jan 28, 21, 00:00, 3 Years ago
Thu, Dec 24, 20, 00:00, 4 Years ago
Sun, Jun 14, 20, 00:00, 4 Years ago
;