Jump to content
Killersites Forums

Do "for Loops" Ever Stop Running?


Recommended Posts

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>

Link to post
Share on other sites

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 by grabenair
Link to post
Share on other sites

@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?

Link to post
Share on other sites

@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 by Ed4words
Link to post
Share on other sites

@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 :bash:

Link to post
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...