ChatGPT解决这个技术问题 Extra ChatGPT

How to reload page every 5 seconds?

I am converting one layout to html; once I make the changes in code/html/css, every time I have to hit F5. Is there any simple javascript/jQuery solution for this? I.e. after I add the script, reload the whole page every 5 seconds (or some other specific time).


J
Jens
 <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);

I think this is probably a better way of doing it than in javascript.
If the use case was that the page needed to refresh for visitors then I might agree, but for development purposes, having a block of code that you can drop into the page without having to update a URI each time is an advantage. Depending on JS isn't a problem when the developer is the entire target audience.
location.reload(1) will repeat a POST if the current page is the result of a POST. That might not be desirable, if you are looking to replicate the meta refresh in Javascript.
Reloading the current page doesn't require you to specify the page, Just: <meta http-equiv="refresh" content="5">
R
Rid Iculous

To reload the same page you don't need the 2nd argument. You can just use:

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

This triggers a reload every 30 seconds.


It is discouraged when used for redirect. It is not deprecated. (And it comes in very handy when you want to refresh a page and are too lazy to learn ajax :)
This isn't working for me. Tried adding this line to this codepen to no avail. codepen.io/css-support/pen/bspdK
@lololololol You have to add it in the "Stuff for " setting in the HTML's Pen Settings area. It doesn't work if you put it in the actual script.
This is great for when working locally ... thanks!
M
Milan Chheda

For auto reload and clear cache after 3 second you can do it easily using javascript setInterval function. Here is simple code

$(document).ready(function() { setInterval(function() { cache_clear() }, 3000); }); function cache_clear() { window.location.reload(true); // window.location.reload(); use this if you do not remove cache }

Auto reload page and clear cache

and you can also use meta for this

<meta http-equiv="Refresh" content="5">

Page refresh