Jump to content

JS click counter error message in IE8


tinfanide
 Share

Recommended Posts

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

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.

Edited by Deirdre's Dad
Link to comment
Share on other sites

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...