Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setTimeout() Issue

Status
Not open for further replies.

Tim2525

Technical User
Feb 7, 2005
51
0
0
US
Hi All,

Trying to get my code to re-execute a function every 5 secs - setTimeout(urlTest = getThuleInfo(), 5000); Everything works fine for the first loop. After that it just sits there. If anyone has any suggestions, I would appreciate it. Also, I'm try to get the document.write('NO not yet..., ', urlRef); to work. It displays the info ok but won't return to start the next loop. Any pointers?

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>

<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>nav.htm</TITLE>
</HEAD>
<BODY onload="urlInfo();">
<!--<BODY>
<form name="NavCatFrm" method="POST" action="">
	<input type="button" value="get URL" onclick="urlInfo();">
</form>-->
<script>
function urlInfo() {
	var urlTest = "true";
	do {
		alert("IN Do...");
		setTimeout(urlTest = getThuleInfo(), 5000);
		alert("OUT Do... " + urlTest);
		if(urlTest == "false") {
			alert("urlTest is ... " + urlTest);
			break;
		}
	}while (urlTest = "true");
}
	
function getThuleInfo () {	
	var urlRef = parent.frameA.location.href;	
	var myArray = urlRef.split("fg_total.asp?");
	var myInfo = myArray[1];
	
	if(typeof(myInfo) != "undefined") {
		alert("myArray info ... " + myArray);
		urlTest = "false";
		return urlTest;
	}else {
		//alert("Nothing yet... " + urlRef);
		document.write('NO not yet..., ', urlRef);
		//return urlTest = "true";
	}
}
</script>
</BODY>
</HTML>

TIA,
T
 
I got the code to loop. I'm still having an issue with setTimeout() function. I want a 5 sec delay but it calls the function without a delay. Still having an issue with my document.write...

TIA,
T
 
To be completely honest, I'm not sure that you can set variable declarations in a setTimeout call. If it is a global variable you can just as easily set it equal to the returned value in the function itself.

With that said, your problem is that your timeout call is not encapsulating the function in quotations. As I mentioned above, I'm not sure that you can set the urlTest variable in the timeout call, so you may need to play with it a bit:
Code:
setTimeout([COLOR=red][b]"[/b][/color]urlTest = getThuleInfo()[COLOR=red][b]"[/b][/color], 5000);

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
setTimeout(...) only executes once. To get it to loop, either add a setTimeout call INSIDE of getThuleInfo() or change your setTimeout(...) call to setInterval(...).

setInterval(...) is probably the right way to go if you want to start a never-ending loop that you probably don't want to interrupt.

setTimeout(...) is probably the way to go if the loop is only intended for a finite number of iterations. In this case, you would have some if-statement in getThuleInfo(...) to check for the stopping condition. Here you could use clearTimeout(...) or just not do another setTimeout(...). The else-statement would include the next setTimeout(...) call and only executes when the stopping condition has not been met.

'hope that helps.

--Dave
 
Still having an issue with my document.write...
document.write can only be used during the page load process. If you try to use it dynamically after the page has loaded, it will completely rewrite your document, giving you a new blank page with whatever was written. I suggest using a <div> box and write to it using the .innerHTML method (or other DOM methods that adhere to web standards) if you need to display new info to the screen

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Thanks All for the quick replies.

I use the setInterval() and the code executes fine. How do I stop the interval. I used clearInterval() but I'm pretty sure I'm using it incorrectly. Any help appreciated.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>

<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>nav.htm</TITLE>
</HEAD>
<BODY onload="urlInfo();">
<!--<BODY>
<form name="NavCatFrm" method="POST" action="">
	<input type="button" value="get URL" onclick="urlInfo();">
</form>-->
<script>
function urlInfo() {
	var urlTest = "";
	urlTest = setInterval("getThuleInfo()", 5000);
}
	
function getThuleInfo () {	
	var urlRef = parent.thule.location.href;	
	var myArray = urlRef.split("fg_total.asp?");
	//var myInfo2 = myArray[0];
	var myInfo = myArray[1];
	
	if(typeof(myInfo) != "undefined") {
		alert("myArray info ... " + myInfo);
		clearInterval(5000);
	}else {
		alert("Nothing yet... " + urlRef);
	}
}
</script>
</BODY>
</HTML>
 
Make urlTest a global var and then change your clearInterval(...) function call to:

Code:
clearInterval(urlTest);

That should do it.

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top