Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto refresh page every 30 seconds

I have a JSP page which has to display the status of various jobs that are running. Some of these jobs take time, so it takes a while for their status to change from processing to complete.

Is it a good idea to have a javascript function that would refresh the page every 30 seconds or so? Are there any ramifications for having a script that is constantly refreshing a page?

The other option is to have a refresh button which on click would refresh the page.

like image 613
Mary Avatar asked Oct 02 '15 18:10

Mary


People also ask

How do I refresh a web page every 30 seconds?

If you really want to do it with JavaScript, then you can refresh the page every 30 seconds with Location. reload() (docs) inside a setTimeout() : window. setTimeout( function() { window.

Can you set a web page to automatically refresh?

It's as simple as going to your browser's app/extension store and finding one you like: Launch your browser. Go to app/extension store (Chrome Web Store, Firefox Add-Ons, Microsoft Edge Add-ons Store, etc.). Enter “auto-refresh” in the search bar.

How do you refresh a page every 5 seconds?

setTimeout(function () { location. reload(1); }, 5000);


2 Answers

There are multiple solutions for this. If you want the page to be refreshed you actually don't need JavaScript, the browser can do it for you if you add this meta tag in your head tag.

<meta http-equiv="refresh" content="30"> 

The browser will then refresh the page every 30 seconds.

If you really want to do it with JavaScript, then you can refresh the page every 30 seconds with Location.reload() (docs) inside a setTimeout():

window.setTimeout( function() {   window.location.reload(); }, 30000); 

If you don't need to refresh the whole page but only a part of it, I guess an AJAX call would be the most efficient way.

like image 181
jeerbl Avatar answered Sep 18 '22 12:09

jeerbl


Just a simple line of code in the head section can refresh the page

<meta http-equiv="refresh" content="30">

although its not a javascript function, its the simplest way to accomplish the above task hopefully.

like image 25
404 Avatar answered Sep 20 '22 12:09

404