![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
Repeating
<script language="javascript">
document.write(new Date().getSeconds()) </script> How would I get JS to repeat this so it would be counting up the seconds? |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4
![]() |
If you want the whole page just to reload this is easy. Use the HTML:
<META http-equiv="refresh" content="1;url=<url of page>"> replacing <url of page> with the url; e.g. <META http-equiv="refresh" content="1;url=thispage.html"> if the page was called "thispage.html". If you want this to just be in part of the page and leave the rest alone, just create a document containing only this and put it in an IFRAME in the document. Arguably a neater way to do it is with a text field; <FORM><INPUT type="text" onFocus="blur()"></FORM> Then your JS code can (assuming this is the first form on the page) set the value of document.forms[0].elements[0].value to write the date; (The onFocus="blur()" bit means that if your user clicks in the field as if to edit it, the browser kicks them right back out. This is as close to a read-only text field as you can get) document.forms[0].elements[0].value = new Date().getSeconds(); To get it to do this once a second, create a function and use a Timeout; e.g. function updateClock() {
document.forms[0].elements[0].value = new Date().getSeconds();
setTimeout("updateClock()", 1000);
}(I might have the arguments to setTimeout the wrong way around, by the way; I always seem to do that. If you get an error from that line, swap them and it should work!) It would be more efficient not to keep creating a new Date object, but that means counting seconds yourself so I'll leave it at that. Hope this helps! Last edited by mackenga; Mar 31st, 2005 at 10:32 AM. Reason: Hadn't explained onFocus="blur()" |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|