Automatically Refreshing Page Every 60 Seconds Using JavaScript

Automatically refreshing page every 60 seconds using JavaScript

setTimeout function call only once so use setInterval function

setInterval("location.reload(true);", 60000);

it will refresh your page after one minute.

Auto reload route every 60 seconds

I strongly suggest you not to refresh the page every 60 seconds for many reason :

  1. It goes against AJAX which is about only load what you need and no more than that.
  2. If your idea is about live data, you should use websockets. Everytime you have new json data, then updating it on frontend. This will solve your problem in a correct way.

How to refresh page every 60 seconds without losing existing data in div?

Instead of reloading the entire page you could just make the AJAX call every 60 seconds instead. To achieve that put the AJAX call in a function and use a setTimeout() call in the success callback of the AJAX request to invoke the function again with the given delay.

jQuery($ => {
var orderNo = "<?php echo $orderNo; ?>";

function getData() {
$.ajax({
url: 'action.php',
method: 'post',
data: {
orderNo: orderNo
},
success: function(response) {
$("#progressbar").append(response);
setTimeout(getData, 60000); // repeat in 60 secs, if AJAX was successful
}
});
}

getData(); // on load
});

Note that the key/value pairs in the object you provide need to be separated by :, not ,. I presume this was just a typo in the question.

How to automatically reload a page after a given period of inactivity

If you want to refresh the page if there is no activity then you need to figure out how to define activity. Let's say we refresh the page every minute unless someone presses a key or moves the mouse. This uses jQuery for event binding:

<script>
var time = new Date().getTime();
$(document.body).bind("mousemove keypress", function(e) {
time = new Date().getTime();
});

function refresh() {
if(new Date().getTime() - time >= 60000)
window.location.reload(true);
else
setTimeout(refresh, 10000);
}

setTimeout(refresh, 10000);
</script>

How to refresh a page every 60 seconds to reload mysql query

You can simply use setInterval for that purpose.

setInterval(function () { 
//Call the server api
}, 1000 * 60);

Browser-refresh has different purpose:

This module improves productivity by enabling instant web page
refreshes anytime a front-end resource is modified on the server.

Basically (very similar to nodemon) it tracks changes in file system and supports live reloading of CSS and JavaScript.

How to reload page every 5 seconds?

 <meta http-equiv="refresh" content="5; URL=http://www.yourdomain.com/yoursite.html">

If it has to be in the script use setTimeout like:

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


Related Topics



Leave a reply



Submit