Asp Net Source.com
What's ASP.NET?

AspNetSource.com's BLOG

AJAX, ASP.NET, C#, CSS, Design, .NET Framework, Javascript, SQL, VB.NET, etc.

automatic refresh page in some interval with javascript

Posted by Tihomir Ivanov on 16 March 2009 05:33
Rating: 8.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>

 Comments:
ph Posted by SQL course on 12 January 2011 21:24   Rating: 8
For me this is tricky. Refreshing automatically? Great logic in creating this code.
us Posted by SEO on 03 August 2009 07:15   Rating: -
That's great, I never thought about automatic refresh page in some interval with javascript like that before.
 
Our Sponsors:  Asp.net file upload component  |   Flash file uploader  |  ASP.NET Hosting