User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 391,668 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,976 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 2685 | Replies: 7
Reply
Join Date: Nov 2005
Posts: 7
Reputation: Sukanto is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Sukanto's Avatar
Sukanto Sukanto is offline Offline
Newbie Poster

Troubleshooting problem with setTimeout()

  #1  
Apr 10th, 2007
I am unable to debug the following code. I can't find any errors here.

I am getting the button but after clicking on it, only Time Remaining: 10000 msecs is being displayed.

In Firefox 2.0.0.3: I checked the error console, it shows timedCount is not defined on line t = setTimeout("timedCount()",200)

IE 7 : gives
Line: 1
Char: 1
Error: Object Expected
Code: 0

I also tried an example of setTimeout() from a website, it worked properly.

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var rem=0
  5. var t
  6.  
  7. function startT(t)
  8. {
  9. rem = t
  10. timedCount()
  11. }
  12.  
  13. function timedCount()
  14. {
  15. if(rem==0)
  16. {
  17. document.write("Time Over.<br>")
  18. return
  19. }
  20. document.write("Time Remaining: " + rem + " msecs.<br>")
  21. rem = rem - 1
  22. t = setTimeout("timedCount()",200)
  23.  
  24. }
  25. </script>
  26. </head>
  27. <body>
  28. <br>
  29. <input type="button" value="Start" onClick="startT(10000)">
  30. </body>
  31. </html>


Please help.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2007
Posts: 83
Reputation: rgtaylor is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
rgtaylor rgtaylor is offline Offline
Junior Poster in Training

Re: problem with setTimeout()

  #2  
Apr 10th, 2007
Your problem here is that you are writing a new document when the button is clicked... the current document is closed, so the browser tries to do the document.write as a new document.

You should use some editable field, like using a text input field for output or a text area for output or an inline frame so you can write to it...
Reply With Quote  
Join Date: Mar 2007
Posts: 83
Reputation: rgtaylor is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
rgtaylor rgtaylor is offline Offline
Junior Poster in Training

Re: problem with setTimeout()

  #3  
Apr 10th, 2007
By the way, I checke dthe web site where you got the code from, but the code on that site won't runn right in most modern browsers...

Peace,
Reply With Quote  
Join Date: Nov 2005
Posts: 7
Reputation: Sukanto is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Sukanto's Avatar
Sukanto Sukanto is offline Offline
Newbie Poster

Re: problem with setTimeout()

  #4  
Apr 10th, 2007
Originally Posted by rgtaylor View Post
Your problem here is that you are writing a new document when the button is clicked... the current document is closed, so the browser tries to do the document.write as a new document.

You should use some editable field, like using a text input field for output or a text area for output or an inline frame so you can write to it...

Thanks for the help rgtaylor.
Now, my code is running fine.

Thanks once again
Reply With Quote  
Join Date: Mar 2007
Posts: 83
Reputation: rgtaylor is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
rgtaylor rgtaylor is offline Offline
Junior Poster in Training

Re: problem with setTimeout()

  #5  
Apr 10th, 2007
Glad to hear it.... It took me a couple minutes to figure it out... basically by the time you did the call back to your function, the page no longer existsed, thus the function no longer existed.... these kinds of things can be tricky to figure out... I got the clue when I used view source and saw the 1 line output by itself.... ;-)

Good luck,
Reply With Quote  
Join Date: Nov 2005
Posts: 7
Reputation: Sukanto is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Sukanto's Avatar
Sukanto Sukanto is offline Offline
Newbie Poster

Re: problem with setTimeout()

  #6  
Apr 10th, 2007
Actually I didn't knew the proper working of document.write(), i.e it creates a new document every time you call it.

As I am not an HTML/Javascript programmer (basically interested in C, Java) , so never bothered to figure it out earlier.

Thanks once again.
Last edited by Sukanto : Apr 10th, 2007 at 11:15 pm.
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 936
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 47
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

Re: problem with setTimeout()

  #7  
Apr 11th, 2007
Document.write( ) only works reliably in the 'loading' period of your page. Something like this:
<html>
<body>
<h1>Title</h1>
<script>document.write("My page content");</script>
</body>
</html>
Will work how you might expect; but calling document.write( ) after the loading context ( i.e. from a user-invoked event like clicking a button ) has that effect of re-writing your page entirely. I don't know whether that's the officially specified behaviour, but most browsers do that.

document.write( ) is a frowned upon technique these days. anything you can do with it can be done using a server-side script, which can make efficient use of caching; and doesn't depend on the client end to perform page generation.

You can use code as below to get 'seamless' output on a page; this is equivalent to putting the output in a textfield/box/iframe, but the output text won't have special wrapping/rendering rules:
<html>
<head>
<script type="text/javascript">	
	var rem=0
	var t
 
	function startT(t)
	{
		rem = t
		timedCount()
	}
 
	function timedCount()
	{
		if(rem==0)
		{
			document.write("Time Over.")
			return
		}
		document.getElementById("output").innerHTML = "Time Remaining: " + rem + " msecs."
		rem = rem - 1
		t = setTimeout("timedCount()",200)
		
	}
</script>
</head>
<body>
	
	<input type="button" value="Start" onClick="startT(10000)">

<span id="output">?</span>
</body>
</html>
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Nov 2005
Posts: 7
Reputation: Sukanto is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Sukanto's Avatar
Sukanto Sukanto is offline Offline
Newbie Poster

Re: problem with setTimeout()

  #8  
Apr 11th, 2007
Originally Posted by MattEvans View Post
Document.write( ) only works reliably in the 'loading' period of your page. Something like this:
<html>
<body>
<h1>Title</h1>
<script>document.write("My page content");</script>
</body>
</html>
Will work how you might expect; but calling document.write( ) after the loading context ( i.e. from a user-invoked event like clicking a button ) has that effect of re-writing your page entirely. I don't know whether that's the officially specified behaviour, but most browsers do that.

document.write( ) is a frowned upon technique these days. anything you can do with it can be done using a server-side script, which can make efficient use of caching; and doesn't depend on the client end to perform page generation.
Thanks MattEvans :-)

Originally Posted by MattEvans View Post
You can use code as below to get 'seamless' output on a page; this is equivalent to putting the output in a textfield/box/iframe, but the output text won't have special wrapping/rendering rules:
<html>
<head>
<script type="text/javascript">	
	var rem=0
	var t
 
	function startT(t)
	{
		rem = t
		timedCount()
	}
 
	function timedCount()
	{
		if(rem==0)
		{
			document.write("Time Over.")
			return
		}
		document.getElementById("output").innerHTML = "Time Remaining: " + rem + " msecs."
		rem = rem - 1
		t = setTimeout("timedCount()",200)
		
	}
</script>
</head>
<body>
	
	<input type="button" value="Start" onClick="startT(10000)">

<span id="output">?</span>
</body>
</html>

After getting the reply from rgtaylor I applied the same workaround ;-)
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 1:58 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC