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

setTimeout problem 1

Status
Not open for further replies.

disord3r

Programmer
Apr 12, 2002
189
US
I have a problem with setTimeout, and the issue is not readily apparent to me. I have a function that document.writes some stuff based on the system clock. In my <head>, I have this:

Code:
function doSomething(){
   ...
   setTimeout("doSomething()",1000);
}

Later in the page, I have a script block that calls the function, to document.write the stuff in the appropriate place. Very simple:

Code:
<script>
doSomething();
</script>

Essentially, this thing should refresh itself every second, and with each pass, the text will be slightly different; different enough that I can tell that it's still running, because I'll see the text changing every second.

So, the page loads, and doSoemthing() executes as it should the very first time. Then, it reaches the setTimeout() call, and one second later, doSomething() runs a second time. Logic would dictate that if doSomething() ran a second time, setTimeout() should run a second time, which would cause doSomething() to run a third time, and so on.

This doesn't happen. When doSomething() runs the second time and it gets to the point of triggering the setTimeout() for the second time, it bombs with the error that 'doSomething() is not defined'.

What am I missing? How did doSomething() run the second time if, according to setTimeout() on it's second run, doSomething() does not exist?

Thanks in advance.
 
Have you tested this in Firefox (with the Friebug extension installed) to give more feedback on the javascript error? It could be that an error is being thrown before that point. It all smells a bit strange, though.

Can you supply some details of your setup? What OS and what browser(s) are you testing this with?

Maybe a cut-down minimal test harness that does something like an alert (in place of your ... in the doSomething() method).

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
I'm on XP Pro with SP2. So far, I've been doing most of my testing with Firefox (2.0.something), simply because it will actually tell me where the error is. IE6 only says 'line 1, char 1: object expected'. Same behavior in IE: doSomething() happens, setTimeout() happens, doSomething() happens, setTimeout claims doSomething() is not defined.

I've never used firebug, but I'll give that a shot. I'm interested to see what details it can give me.

Regarding the alert suggestion, I'm not sure what you're getting at there. The main problem is that I need to call setTimeout from within the same function that setTimeout calls. I don't know if this is technically recursion, but I need this process to go on for as long as the user cares to stay on the page. If I change my code so that setTimeout pops an alert() instead of my function, it's only going to happen once and then be done. I don't know what I would learn from that.

I already know my doSomething() function is being called at least twice, but I don't know why it suddenly becomes non-existent according to setTimeout().

I'll let you know how firebug turns out.
 
Firebug shows exactly the same details as the built-in error console. I think this will be a very helpful tool for other things in the future, so thanks for mentioning that.

As for my original problem, I'm still in the same boat - setTimeout() works only once, and after that, it fails to see the function from which it is being called.


/<
 
Code:
<html>
<head>
	<title>Test</title>
	<script>
	function doSomething(){
		var currentDate = new Date();
		document.write(currentDate);
		setTimeout("doSomething()",1000);
	}
        </script>
</head>
<body>

<script>
doSomething();
</script>

</body>
</html>

The above code does not work, and I would like to know why it doesn't work.

1. A script block in the body calls the doSomething() function
2. The current time is displayed by document.write
3. The setTimeout() call waits one second, and calls doSomething() again
4. The time is updated by one second when it hits the document.write on the second time through doSomething()
5. the setTimeout() call waits one second, and generates an error saying that doSomething is not defined.

So, the guts are a little different than my actual function, but the result is exactly the same: setTimeout() is called successfuly once, and then after that it generates an error, causing the page execution to stop.

Thanks in advance.
 
Hi

Once the [tt]document[/tt] is finished, you can note [tt]write()[/tt] into it anymore. Any further attempt will reset the current document and your function will gone.

If you not use the [tt]document.write()[/tt] after the [tt]document[/tt] was finished, your script works :
Code:
<html>
<head>
    <title>Test</title>
    <script>
    function doSomething(){
        var currentDate = new Date();
        [red]document.title=currentDate;[/red]
        setTimeout("doSomething()",1000);
    }
        </script>
</head>
<body>

<script>
doSomething();
</script>

</body>
</html>

Feherke.
 
With that knowledge, I wrapped a div around the second script block and, rather than using document.write to send text to the screen, I'm using the div's innerHTML property to pipe the text into that.

Works like a charm!

Code:
<html>
<head>
	<title>Test</title>
	<script>
	function doSomething(){
		var currentDate = new Date();
		[COLOR=red]document.getElementById('something').innerHTML = currentDate;[/color]
		setTimeout("doSomething()",1000);
	}

</script>
</head>
<body>

[COLOR=red]<div id="something">[/color]
<script>
doSomething();
</script>
[COLOR=red]</div>[/color]

</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top