automatic refresh page in some interval with javascript
Posted by Tihomir Ivanov on 16 March 2009 05:33
Rating: 1.00
Hi all,
in this article you can add functionality to your page for automatic refreshing in some interval.
To add this functionality, just copy-paste the code below between <head> tags of your page.
<script language="javascript">
var refreshinterval = 5 //interval in seconds for refreshing
var displaycountdown="yes" // dispalys in status bar seconds for refreshing
var starttime
var nowtime
var reloadseconds=0
var secondssinceloaded=0
function starttime()
{
starttime=new Date()
starttime=starttime.getTime()
countdown()
}
function countdown()
{
nowtime = new Date()
nowtime = nowtime.getTime()
secondssinceloaded = (nowtime-starttime) / 1000
reloadseconds = Math.round(refreshinterval-secondssinceloaded)
if (refreshinterval >= secondssinceloaded)
{
var timer = setTimeout("countdown()", 1000)
if (displaycountdown == "yes")
{
window.status="Page refreshing in "+reloadseconds+ " seconds"
}
}
else
{
clearTimeout(timer) window.location.reload(true)
}
}
window.onload=starttime
</script>
That's great, I never thought about automatic refresh page in some interval with javascript like that before.