homepage Welcome to WebmasterWorld Guest from 54.166.66.204
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
refreshing JavaScript?
keyplyr




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

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>

 

Purple Martin




msg:1483813
 6:03 am on Sep 11, 2002 (gmt 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

keyplyr




msg:1483814
 6:21 am on Sep 11, 2002 (gmt 0)


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.

Purple Martin




msg:1483815
 6:50 am on Sep 11, 2002 (gmt 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.

keyplyr




msg:1483816
 8:12 am on Sep 11, 2002 (gmt 0)

Thanks again, I'll play around with it.

rewboss




msg:1483817
 9:39 am on Sep 11, 2002 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved