•
•
•
•
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
![]() |
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
I also tried an example of setTimeout() from a website, it worked properly.
Please help.
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.
html Syntax (Toggle Plain Text)
<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.<br>") return } document.write("Time Remaining: " + rem + " msecs.<br>") rem = rem - 1 t = setTimeout("timedCount()",200) } </script> </head> <body> <br> <input type="button" value="Start" onClick="startT(10000)"> </body> </html>
Please help.
•
•
Join Date: Mar 2007
Posts: 83
Reputation:
Rep Power: 0
Solved Threads: 2
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...
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...
•
•
•
•
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
•
•
Join Date: Mar 2007
Posts: 83
Reputation:
Rep Power: 0
Solved Threads: 2
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,
Good luck,
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.
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.
•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 936
Reputation:
Rep Power: 5
Solved Threads: 47
Document.write( ) only works reliably in the 'loading' period of your page. Something like this:
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>
<body>
<h1>Title</h1>
<script>document.write("My page content");</script>
</body>
</html>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.
•
•
•
•
Document.write( ) only works reliably in the 'loading' period of your page. Something like this:
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.<html> <body> <h1>Title</h1> <script>document.write("My page content");</script> </body> </html>
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>
After getting the reply from rgtaylor I applied the same workaround ;-)
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
- Javascript problem (JavaScript / DHTML / AJAX)
- setTimeout prevents resetting of image (JavaScript / DHTML / AJAX)
- Javascript Problem with Firefox (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Navbar Transparency Issue
- Next Thread: how to run javascript program


Linear Mode