Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 25th, 2005, 12:46 PM   #1
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
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?
Navid is offline   Reply With Quote
Old Mar 31st, 2005, 10:30 AM   #2
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
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()"
mackenga is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:39 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC