Forum Moderators: open

Message Too Old, No Replies

An endless "for" loop

for loops in javascript, endless loops

         

BungeeJumper

2:47 am on Aug 6, 2009 (gmt 0)

10+ Year Member



I'm having a problem where this real simple for loop is running endlessly in Firefox. After clicking the "Change Text" button the one line in the for loop - document.writeln(i); - correctly writes 0 1 2 3 4 but the browser keeps running in an endless loop trying to do who knows what and the script does not continue on to finish loading the rest of the page.
If I comment out the document.writeln(i); inside the loop the page loads fine

<script type="text/javascript">
document.write("Hello World!");
myArray = new Array(15,89,4,61,5)
catArray = new Array(60,65,69,2719)
document.writeln(myArray);
document.writeln(catArray);

var oFullParent = document.getElementById('69');
//document.writeln(oFullParent);
document.writeln(oFullParent.firstChild.nodeValue);

function changeText(){

var arLen=myArray.length;
for ( var i=0, len=arLen; i<len; ++i ){
document.writeln(i);
var a = i;
}
/*document.getElementById('60').innerHTML = '15';*/
}
</script>
<p>Welcome to the site <b id='69'>dude</b> </p>
<input type='button' onclick='changeText()' value='Change Text'/>

<?
echo '<div id="copy">
<button onclick="dochangepops(\'srcl\')">Get CD info</button>
</div>';

Any help in this would be greatly appreciated.

eelixduppy

3:15 am on Aug 6, 2009 (gmt 0)



I cannot see any reason for that loop to keep going indefinitely. Are you sure there isn't anything else going on here?

BungeeJumper

3:53 am on Aug 6, 2009 (gmt 0)

10+ Year Member



You're right, it only does it in Firefox. It loads fine in Opera and Konqueror. I just downloaded a plugin for Firefox that is supposed to let you switch user agents. That might be the cause, as my Gmail account looks screwy and I get rrequest for Google to store data on my pc. I think they have enough data storage without using mine lol. Well thanks for gander at it.

daveVk

3:54 am on Aug 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using document.write(ln) after page onload is the problem. If this is for debug remove it, or replace with modification of page via say innerHTML.

BungeeJumper

4:04 am on Aug 6, 2009 (gmt 0)

10+ Year Member



k, thank you Dave. Yes, it is entirely debug. I'm trying to get a better handle on how to actually debug in javascript. I'm having problems iterating through arrays but I'll do another post for the different topic