Edscript Posted March 10, 2013 Report Share Posted March 10, 2013 When this code runs the browser running circle (in the tab display) never times out it just keeps running, why? <head> <script language="javascript" type="text/javascript"> function products_list() { var products = new Array(); products[0] = "hair spray"; products[1] = "shoes"; products[2] = "flowers"; products[3] = "socks"; for (var i = 0; i < products.length; i++) { document.write(products + "<br>"); } } </script> </head> <body> <h3 onclick="products_list()"> -> Show List</h3> </body> Quote Link to comment Share on other sites More sharing options...
grabenair Posted March 10, 2013 Report Share Posted March 10, 2013 (edited) You NEED to put a break in the code to stop or will keep going for ever and can cause problems. You can find a the info you need here, http://php.net/manua...tures.break.php You good also Google, stoping a for loop from running in php, I came up with a few ways of doing this besides the break. Edited March 10, 2013 by grabenair Quote Link to comment Share on other sites More sharing options...
falkencreative Posted March 10, 2013 Report Share Posted March 10, 2013 @grabenair: You NEED to put a break in the code to stop or will keep going for ever and can cause problems. Not quite true -- a for loop has an automatic break built in. In the code above, it should break after i is no longer less than products.length. (We're also dealing with Javascript here, not PHP.) @Ed4Words: When I run that code (and assuming I click on the header, obviously), the system prints out the four array items, then stops. As far as I can tell, it's working correctly. What is happening that makes you think the code is still running? Quote Link to comment Share on other sites More sharing options...
Edscript Posted March 10, 2013 Author Report Share Posted March 10, 2013 (edited) @grabenair: Not quite true -- a for loop has an automatic break built in. In the code above, it should break after i is no longer less than products.length. (We're also dealing with Javascript here, not PHP.) @Ed4Words: When I run that code (and assuming I click on the header, obviously), the system prints out the four array items, then stops. As far as I can tell, it's working correctly. What is happening that makes you think the code is still running? In the tab on my browser there is a running circle with the tab to the web page reading "connecting" and it doesn't seem to stop. If I run the script in the body; then it loads with the page everything is normal, but of course the heading is over written. But if I call the function which I named "products_list()" from the head with an "onclick" in the heading title, then I get a "connecting" message in the tab of the webpage that never seems to stop, although the web page does display the array values correctly. Edited March 10, 2013 by Ed4words Quote Link to comment Share on other sites More sharing options...
Edscript Posted March 10, 2013 Author Report Share Posted March 10, 2013 @grabenair: Not quite true -- a for loop has an automatic break built in. In the code above, it should break after i is no longer less than products.length. (We're also dealing with Javascript here, not PHP.) @Ed4Words: When I run that code (and assuming I click on the header, obviously), the system prints out the four array items, then stops. As far as I can tell, it's working correctly. What is happening that makes you think the code is still running? Yeh, another strange thing - it does seem to work fine in my explorer 9 browser on my desktop, but the on running "connecting" message in the tab of the page is active in my Fire Fox browser on my laptop. Strange??! I always though of Monzilla as being javascript sensitive in there applications - that is they would be more prone to being bug free as far as running javascript. Or maybe I have been hacked Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.