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 :
- It goes against AJAX which is about only load what you need and no more than that.
- 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
How to Implement Ping/Pong Request for Websocket Connection Alive in JavaScript
Json.Stringify Without Quotes on Properties
Postman Test - Validating String Values in an Object in an Array
Map, and Removing Commas from an Array in JavaScript
How to Append Whole Set of Model to Formdata and Obtain It in MVC
How to Check If Checkbox Is Checked With Protractor, Cucumberjs and Chai
How to Handle Multiple Submit Buttons in a Form Using Angular Js
Updating Value of Multidimensional Array of Object
Convert a Flat Json File to Tree Structure in JavaScript
Javascript Function (Input Date Bigger Than Today Date)
How to Get the Average from Array of Objects
Hosting Your Own JavaScript Scripts Files (Other Than Jquery) on Fast Free Cdns Like Google
Onchange Event Updates State With 1 Character Delay
Removing Currency Symbol and Replacing Comma With Point Using Pure JavaScript
Angular 6 Store Data from Json Array Inside Json
Jquery Validation Plugin - Typeerror: $(...).Validate Is Not a Function