[thelist] JS Functions: calling themselves et al

Tom Dell'Aringa pixelmech at yahoo.com
Wed Nov 13 13:44:01 CST 2002


hey all,

I have the following function, which takes a preset time and counts
down to zero and pops an alert. Here it is:
===========================================
<html>
<head>
<script language="JavaScript">

var countMin, countSec;

function intialize()
{
	countMin = 0;
	countSec = 5;
	counting();
}

function Display(min,sec)
{
	var disp;

	if(min<=9) disp = " 0";
		else disp=" ";

	disp+=min+":";

	if(sec<=9) disp+="0"+sec;
		else disp+=sec;

	return(disp);
}

function foo(){}

function counting()
{
	countSec--;
	if(countSec==-1) { countSec=59; countMin--; }

	document.forms['countdown'].elements['cdDisplay'].value =
Display(countMin,countSec);

	if((countMin==0)&&(countSec==0)) alert("Reload");
		else foo = setTimeout("counting()",1000); //can't call itself
}
</script>
</head>
<body>
<form name="countdown">
<input type="button" value="Start" onclick="intialize()">
<input type="text" name="cdDisplay" size="9" style="border: none;
background: #f1f1f1 ">
</form>
</body>
</html>
===================================================

Question:

Note the foo() function which is empty. The counting() function
essentially wants to call itself and continue counting down, but if I
set the line

else foo = setTimeout("counting()",1000);

to be
else counting = setTimeout("counting()",1000);

I get an error. It seems as though it can't call itself. I was
wondering if anyone could explain this and if there is a way around
it. I guess its a recursive thing I am after.

Also, could use suggestions - I will have multiple instances of
counters on a page that all need to use this script. I'm wondering if
I should make it an object or if there is someway to have them all
use the same script.

TIA

Tom

=====
var me = tom.pixelmech.webDeveloper();

http://www.pixelmech.com/
http://www.maccaws.com/
[Making A Commercial Case for Adopting Web Standards]

__________________________________________________
Do you Yahoo!?
U2 on LAUNCH - Exclusive greatest hits videos
http://launch.yahoo.com/u2



More information about the thelist mailing list