Javascript function to reload page every X seconds?

A few questions:

  • I never used JS listeners other than onclick and onkey , so I thought, could someone help me with what I need to reload the page every X seconds?

  • Secondly, the page contains a minimum, literally only one input field. Should I include html head and body ?

+7
javascript syntax html
source share
4 answers

You do not need Javascript for this simple function. Add to page title:

 <meta http-equiv="Refresh" content="300"> 

300 is the number of seconds in this example.

+21
source share

To reload the page after 5 seconds (5000 milliseconds) using JavaScript, add the following to the bottom of the page:

 <script type="text/javascript"> setTimeout(function () { location.reload(true); }, 5000); </script> 

As Greg Hewgill points out, you can also do this with the meta refresh tag:

 <meta http-equiv="Refresh" content="5"> 

Strictly speaking, you still need the <html> and <body> tags. Some browsers may display the page correctly without them, but you can enable them.

+18
source share

use the timer: http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/ and use ajax to reload if it is dynamic

+1
source share

To your second question, you definitely do !!

Toy is your first question check this out:

http://www.javascriptkit.com/script/script2/autofresh.shtml

Or do it without javascript:

http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm

It does what you requested and it is very easy to configure!

0
source share

All Articles