21

here's my problem: i need to display a message for a while, and then reload the page. can someone tell me how to reload a page, after certain delay?

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
vnay92
  • 359
  • 1
  • 2
  • 6

3 Answers3

47

You don't even need jQuery or HTML5 for this:

setTimeout(location.reload.bind(location), 60000);

This will wait 1 minute (60,000 milliseconds), then call the location.reload function, which is a built-in function to refresh the page.

Legionar
  • 7,472
  • 2
  • 41
  • 70
Scimonster
  • 32,893
  • 9
  • 77
  • 89
  • 2
    Results in "Uncaught TypeError: Illegal Invocation". See answer from Amin Jafari, it is easy to wrap this in an anonymous function and for it to be a functional solution. – ryanm Jun 14 '16 at 19:15
  • Looks good, a nice solution (tighter than an anonymous function). – ryanm Jun 15 '16 at 16:18
24
setTimeout(function(){
    window.location.reload(); // you can pass true to reload function to ignore the client cache and reload from the server
},delayTime); //delayTime should be written in milliseconds e.g. 1000 which equals 1 second

Update:

One-liner using ES6:

setTimeout(() => window.location.reload(), delayTime);
Amin Jafari
  • 7,157
  • 2
  • 18
  • 43
6

You may try this without js, it cycles:

<meta http-equiv="refresh" content="5"/> <!-- 5 sec interval-->
<h1>Page refersh in every 5 seconds...</h1>

You can even navigate to a different page, visiting google home page

<meta http-equiv="refresh" content="5;http://www.google.com"/> <!-- 5 sec delay-->
<h1>Redirecting in 5 seconds...</h1>
  • And what if the design is like, when user clicking on a button a message should display, and after displaying that message, in a delay of 5 secs the page should be reloaded..? Will this work in that context.? – Rajaprabhu Aravindasamy Jun 25 '14 at 05:46
  • @RajaprabhuAravindasamy, OP didn't mentioned this as requirement in his post. Of course what you have said will require a different solution, possibly `window.setTimeout`. –  Jun 25 '14 at 05:51