Jump to content


Photo

Do "for Loops" Ever Stop Running?


  • Please log in to reply
4 replies to this topic

#1 Ed4words

Ed4words

    Member

  • Member
  • PipPip
  • 18 posts

Posted 09 March 2013 - 11:01 PM

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[i] + "<br>");
}
}

</script>
</head>

<body>
<h3 onclick="products_list()"> -> Show List</h3>
</body>
  • 0

#2 grabenair

grabenair

    Advanced Member

  • Member
  • PipPipPipPip
  • 361 posts
  • LocationIowa

Posted 10 March 2013 - 10:32 AM

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, 10 March 2013 - 10:38 AM.

  • 0
Standards will change. But your website needs to remain in serving its audience.
My Portfolio site

#3 Ben

Ben

    Administrator

  • Administrators
  • 5,682 posts
  • LocationGainesville, Florida

Posted 10 March 2013 - 11:16 AM

@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?
  • 0
Benjamin Falk
Falken Creative : Twitter

#4 Ed4words

Ed4words

    Member

  • Member
  • PipPip
  • 18 posts

Posted 10 March 2013 - 04:29 PM

@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, 10 March 2013 - 04:32 PM.

  • 0

#5 Ed4words

Ed4words

    Member

  • Member
  • PipPip
  • 18 posts

Posted 10 March 2013 - 05:11 PM

@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:
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

★★★★★ 5 Star Rated Web Developer Course - check it out now!