Jump to content

Delayed infinte loop


Colton.Wagner

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.