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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Functions that call themselves

Status
Not open for further replies.

james6848

Technical User
May 10, 2004
23
GB
Just trying to create some moving layers, and came across this code in a book:

Code:
 function showlayer() {
	var hiddenlayer = document.getElementById('tablayer');
	var layerposition = parseInt(hidden.style.left);
	
	if (layerposition < 0) {
		hiddenlayer.style.left = (layerposition + 5) + "px";
		setTimeout("showlayer()", 20);
	}
}

I find it counter-intuitive somehow to have the function call itself within the function [setTimeout("showlayer()"] - I feel as if the function doesn't yet exist at this point, so how can it be called? Am I wrong? Should I have some coffee?!
 
Look at thread216-976670 and you will see where a function might want to call itself...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook

zen.gif

 
Thanks for the link. So, I take it that the function is set up completely before the IF statement is run.
 
Yeah, it's fine, it works fine.

Take this simple example (warning, it might be slightly annoying to close):

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]

<html>
<head>
<title>Untitled</title>

<script language="javascript"><!--

function doSomething() {
    alert('hi');
    doSomething();
}

--></script>
</head>

<body onload="doSomething();">

Hey there.

</body>
</html>

You don't even need to set a timeout.

*cLFlaVA
----------------------------
[tt]Sigs cause cancer.[/tt]
 
I see! I just need to remember it's OK to do this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top