21

A couple of questions:

  • I've never really used JS listeners other than onclick and onkey events, so I wondered if someone could help me with what I need in order to reload the page every X seconds?

  • Secondly, the page contains bare minimum, literally just one input box. Do I still need to include the html head and body?

alexg
  • 3,015
  • 3
  • 23
  • 36
Tom
  • 541
  • 3
  • 10
  • 17

4 Answers4

42

You don't need Javascript for this simple function. Add in the page header:

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

300 is the number of seconds in this example.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
32

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 notes, you can also accomplish this with the meta refresh tag:

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

Strictly speaking, you do still need <html> and <body> tags. Some browsers may render the page correctly without them, but your are safest to include them.

Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102
pkaeding
  • 36,513
  • 30
  • 103
  • 141
  • The java script option gives me an error: `Uncaught ReferenceError: settimeout is not defined ` – Zapnologica Feb 28 '14 at 07:35
  • 1
    @Zapnologica: the second 't' in `setTimeout` is capitalized. – pkaeding Mar 02 '14 at 23:33
  • Very important that if you want it to refresh every X seconds, you will need to use the setInterval(function(){}, X); not setTimeout(), where the anonymous function in setInterval is run every X miliseconds – namore Nov 20 '19 at 17:19
2

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

Bart
  • 4,830
  • 16
  • 48
  • 68
0

To your second question, ye you definitely do!!

Toy your first question check this out:

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

Or this to do it without javascript:

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

It does what you asked and is very easy to configure!

Trufa
  • 39,971
  • 43
  • 126
  • 190