tinfanide Posted April 30, 2011 Share Posted April 30, 2011 Just for practice of JS I wrote a click counter and came across something accidental:IE 8: "IE stack overflow at line 12"When I clicked to 2Firefox 4.0.1: works properly, no error messageI've googled it to see if anyone did come across it but no exact reply to it.Of course it does not matter much to me but just out of curiosity want to know why.Anyone who feels interested in debugging JS please do have a look!Thanks. <script type ="text/javascript">var x = 0;function count(){x += 1;if(x>=3){stop(count())}document.getElementById( "counting" ).value = x;}</script><input id = "counting" type = "text" /><input type ="button" value = "Click to count how many times you click me!" onclick = "count()"/> Link to comment Share on other sites More sharing options...
jeffman Posted April 30, 2011 Share Posted April 30, 2011 the stop function doesn't do what you think it does. The problem is that stop(count()) executes count. So if x is greater than 3, count executes and just keeps getting called, over and over. You don't notice it in FF because it catches the problem and throws a "too much recursion" error, which you can see in your console. The sensible thing would be this: if (x >= 3){ return;} And to always check your console (Web Console or Error Console, depending on your version). Read my profile for more info on that. Link to comment Share on other sites More sharing options...
tinfanide Posted May 1, 2011 Author Share Posted May 1, 2011 the stop function doesn't do what you think it does. The problem is that stop(count()) executes count. So if x is greater than 3, count executes and just keeps getting called, over and over. You don't notice it in FF because it catches the problem and throws a "too much recursion" error, which you can see in your console. The sensible thing would be this:if (x >= 3){ return;} And to always check your console (Web Console or Error Console, depending on your version). Read my profile for more info on that. Yes, I worked out how stop() produces recursion.Yes, console is a good tool. Thanks for your profile info. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.