Colton.Wagner Posted April 21, 2011 Share Posted April 21, 2011 I am trying to make a script that changes data in a div container every 6000 milliseconds. The php code is correct and I will have no problems fixing that if there are any tweaks needed. So far this is what I have. function variation(str){ var xmlhttp; if (str.length==0){ document.getElementById("variation").innerHTML=""; return; } if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else{ // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ document.getElementById("variation").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","./includes/variation.php?id="+str,true); xmlhttp.send(); } function variationTimer(){ for(i=1;i<4;i++){ variation(i); i++; if (i>=3){ i = 1; } setTimeout("variationTimer()",6000); } } I am very aware that it is an infinite loop however I want this to be endless so I do not want it to stop after so many revolutions. Any help would be much appreciated thank you! Quote Link to comment https://forums.phpfreaks.com/topic/234380-delayed-infinte-loop/ Share on other sites More sharing options...
trq Posted April 22, 2011 Share Posted April 22, 2011 What is your question? Quote Link to comment https://forums.phpfreaks.com/topic/234380-delayed-infinte-loop/#findComment-1204731 Share on other sites More sharing options...
Colton.Wagner Posted April 22, 2011 Author Share Posted April 22, 2011 Sorry, Why when I run this script or call the function does it cause latency and freeze the browser? Is there a better way to delay an infinite loop? As of right now it looks like I just programmed a processing nightmare. Thanks again for any help. Quote Link to comment https://forums.phpfreaks.com/topic/234380-delayed-infinte-loop/#findComment-1204739 Share on other sites More sharing options...
DavidAM Posted April 22, 2011 Share Posted April 22, 2011 function variationTimer(){ for(i=1;i<4;i++){ variation(i); i++; if (i>=3){ i = 1; } setTimeout("variationTimer()",6000); } } This function is running an infinite FOR loop which is calling setTimeout ON EVERY PASS THROUGH THE LOOP. In very short order you have set hundreds of timers. And as those timers fire, they are each starting ANOTHER infinite FOR loop and setting more timers, which are starting more infinite FOR loops and setting more timers, which are starting more infinite FOR loops and setting more timers, which are starting more infinite FOR loops and setting more timers --- ad infinitum Well, I think that's the case. I'm no JavaScript expert Quote Link to comment https://forums.phpfreaks.com/topic/234380-delayed-infinte-loop/#findComment-1204740 Share on other sites More sharing options...
Colton.Wagner Posted April 22, 2011 Author Share Posted April 22, 2011 That's what I figured I just am not aware of a pause is javascript that doesn't require you to call a new function. Sorry for the confusion. I guess a more appropriate question to ask would be. Is there a way to delay by a set time in javascript then continue from where the pause ended? Thanks again both of you are very helpful. Quote Link to comment https://forums.phpfreaks.com/topic/234380-delayed-infinte-loop/#findComment-1204742 Share on other sites More sharing options...
DavidAM Posted April 22, 2011 Share Posted April 22, 2011 So you want to call variation(1); pause a while call variation(2); pause a while call variation(3); pause a while and so forth? You would need to make the variable i static so it will maintain its value when the function exits. Like I said, I'm no JS expert, but I would do something like this: var i = 1; function variationTimer(){ variation(i); i++; if (i>=3){ i = 1; } setTimeout("variationTimer()",6000); } // Kickoff the timer variationTimer(); Quote Link to comment https://forums.phpfreaks.com/topic/234380-delayed-infinte-loop/#findComment-1204743 Share on other sites More sharing options...
Colton.Wagner Posted April 22, 2011 Author Share Posted April 22, 2011 Correct for the most part it would be possible so long as I did not have to call another function to do so. Quote Link to comment https://forums.phpfreaks.com/topic/234380-delayed-infinte-loop/#findComment-1204746 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.