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);
Auto refresh page every 30 seconds
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.
Javascript function to reload a page every X seconds?
You don't need Javascript for this simple function. Add in the page header:
<meta http-equiv="Refresh" content="300">
300 is the number of seconds in this example.
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>
Reload page every 5 seconds - external JS
That is because you are looking for
window.setInterval(function(){refreshPage()}, 5000);
Could also just call the function as noted in the comments:
window.setInterval(refreshPage, 5000);
How to refresh a page after some seconds with jquery?
You can simply use setTimeout
:
setTimeout(function() {
location.reload();
}, 5000);
Note that setTimeout
does not guarantee exact 5 second delay. It is not very accurate.
Related Topics
Generating CSS Media Queries with JavaScript or Jquery
Es6 Module Import Giving "Uncaught Syntaxerror: Unexpected Identifier"
IE9: Why Setting "-Ms-Transform" Works from CSS, But Not with Jquery.Css()
How to Disable Copy Paste (Browser)
Using JavaScript to Increment Top/Left/Bottom/Right Values
Safari: Absolutely Positioned Divs Not Moving When Updated via Dom
Restart a Gif Animation Without Reloading the File
How to Set Textarea Scroll Bar to Bottom as a Default
Get All Computed Style of an Element
Fix First Column of a Bootstrap Table
Change Svg Fill Color in :Before or :After CSS
Affix Is Not Working in Bootstrap 4 (Alpha)
Converting Em to Px in JavaScript (And Getting Default Font Size)