Automatic page refresh every 30 seconds

I have a JSP page that should display the status of various tasks being performed. Some of these tasks take time, so their status requires a transition from processing to completion.

Is it good to have a javascript function that will refresh the page every 30 seconds or so? Are there any consequences for a script that constantly updates the page?

Another option is to have a refresh button that, when clicked, will refresh the page.

+28
source share
4 answers

There are several solutions for this. If you want to refresh the page, you don’t really need JavaScript, the browser can do it for you if you add this tag metato your tag head.

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

The browser will refresh the page every 30 seconds.

If you really want to do this using JavaScript, then you can refresh the page every 30 seconds using location.reload()( docs ) inside setTimeout():

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

Otherwise, if you do not need to refresh the entire page, but only part of it, I think that calling Ajax will be the most efficient way.

+70
source

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

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

although this is not a javascript function, it is the easiest way to accomplish the above task.

+12
source

, , , , , , .

JavaScript:

<script language="javascript">
setTimeout(function(){
   window.location.reload(1);
}, 30000);
</script>
+4

Use setIntervalinstead setTimeout. Although in this case either everything will be fine, but setTimeoutin its essence it works only once, when it setIntervalgoes on endlessly.

<script language="javascript">
setInterval(function(){
   window.location.reload(1);
}, 30000);
</script>
+3
source

Source: https://habr.com/ru/post/1610017/


All Articles