Welcome to WebmasterWorld Guest from 54.211.62.139

Forum Moderators: open

Message Too Old, No Replies

refreshing JavaScript?

     
5:15 am on Sep 11, 2002 (gmt 0)

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:7009
votes: 175


Anyway to get this inline JS to refresh every 10 seconds other than having the page refresh?

<script language="javascript" type="text/javascript">document.write(Date())</script>

6:03 am on Sept 11, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 21, 2002
posts:977
votes: 0


You could do the following:

1. Put a span (with id) in the document where you want something displayed (instead of using a script tag).
2. Write a JavaScript function which changes the innerHTML property of the span to what you want displayed.
3. Make the function call itself every 10 seconds using setTimeout.
4. Call the function with the body onLoad event to get it started.

HTH

6:21 am on Sept 11, 2002 (gmt 0)

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:7009
votes: 175



Thanks Purple_Martin but #2, #3 and #4 are over my head - LOL

Regardless, I was hoping there was just an element to add to the JS string.

6:50 am on Sept 11, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 21, 2002
posts:977
votes: 0


lol, OK, I'll give more info then!

There is nothing you can just add to the code snippet you've got. Once it has s written it's stuff to the document, it's done it's job and that's it. Game over.

So here is some sample code to look at for each of the 4 steps (I'm just typing it straight in without testing so apolgies if I give you errors).

1:

<span id="mySpan"></span>

2 & 3 (put in the head):

<script language="javascript">
function changeMySpan() {
var myDate = "" + new Date(yyyy,mm,dd,hh,mm,ss) // change the format as you like
document.all["mySpan"].innerHTML = myDate
setTimeout("changeMySpan()", 10000) // the number is the delay in milliseconds
}
</script>

4:

<body onLoad="changeMySpan()">

Of course the document.all part won't work in Netscape, so doe a search for browser sniffing scripts and "getElementById" for info about accessing an element's properties in Netscape.

8:12 am on Sept 11, 2002 (gmt 0)

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:7009
votes: 175


Thanks again, I'll play around with it.

9:39 am on Sept 11, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:June 7, 2002
posts:475
votes: 0


An alternative is to leave out the setTimeout method in the function body, and change the onload handler to:

onload="setInterval('changeMySpan()', 10000);"

It's slightly more efficient than using setTimeout to recursively call a function. Well, not so's you'd notice, but it is a bit more elegant.