Forum Moderators: open

Message Too Old, No Replies

I thought I had found just what I was looking for when

I found this recursive function. But it is not working.

         

MarcMiller

7:42 am on Oct 4, 2005 (gmt 0)

10+ Year Member



I have been trying to find a a JavaScript function that will repeatedly run my change image function and when I found a site that gave it to me I thought I had the answer. My own writing of a recursive function kept failing so the comments in code below seemed like the answer I was looking for. However I think I've been following the instructions faithfully but it doesn't work. Below is a quote in a code snippet which can be found at this link [songhaysystem.com...] It is followed by my experimental use of this code snippet which should write the numbers 1 through 10 one line at a time. Firefox will only render the number 1 from my code. The Firefox JavaScript console gives me the following error messages in warnings.

"Warning: assignment to undeclared variable timeoutID
Source File: file:///c:/htmlplay2/pro1LeadImage/whyIwasLookingFor.htm
Line: 15"

Also this error message.

"Error: MyFunc is not defined
Source File: file:///c:/htmlplay2/pro1LeadImage/whyIwasLookingFor.htm
Line: 15"

I would appreciate it very much if somebody could help me get this code going. Below is all that I have mentioned above.

" Many designs using a for or while loop structure may instead have needed the setTimeout() method of the window object calling a function recursively. The general form of this structure is:
<!--
function MyFunc() {
//code here
timeoutID = setTimeout("MyFunc()", 1000)
}
-->
where timeoutID is a script-global variable. After MyFunc() is called, this structure then recursively calls MyFunc() every second until clearTimeout(timeoutID) is used. Unlike a for or while loop, this "loop" is time based."

My code.

<!--
<head>
<title></title>
<script language="JavaScript">
<!--
var c=0;
var timeoutID=0;
function MyFunc() {
c += 1;
window.document.write(c + "</br>");
timeoutID = setTimeout("MyFunc()", 1000);
}
-->
</script>
</head>

<body onload="MyFunc()">

</body>

</html>
-->

Bernard Marx

8:03 am on Oct 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's because you are using document.write. When used after the document has loaded, it overwrites the document, and the script in the process, and thus the functon is no longer available.

In fact this is a slightly convoluted issue, because the delayed command itself isn't wiped out. Let's just leave this as "avoid document.write" for now.

Here's a variation for you.
Note that the value a timeout returns only needs to be saved if you want to cancel it.

<html><head>
<script type="text/javascript">
var c=0;
var timeoutID;
function MyFunc()
{
document.getElementById('counter').innerHTML = ++c;
timeoutID = setTimeout("MyFunc()", 1000);
}
</script>
</head>
<body onload="MyFunc()">
<h1 id="counter">0</h1>
<input type="button" value="stop" onclick="clearTimeout(timeoutID)">
</body>
</html>