Javascript 如何每 5 秒重新加载一次页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2787679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:55:12  来源:igfitidea点击:

How to reload page every 5 seconds?

javascriptjquery

提问by Wasim Shaikh

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).

我正在将一种布局转换为 html;一旦我在 code/html/css 中进行了更改,每次我都必须按 F5。是否有任何简单的 javascript/jQuery 解决方案?即在我添加脚本后,每 5 秒(或其他特定时间)重新加载整个页面。

回答by jAndy

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

If it has to be in the script use setTimeoutlike:

如果必须在脚本中使用setTimeout ,例如:

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

回答by 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.

这会每 30 秒触发一次重新加载。

回答by Shafiqul Islam

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

对于 3 秒后自动重新加载和清除缓存,您可以使用 javascript setInterval 函数轻松完成。这是简单的代码

$(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
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<p>Auto reload page and clear cache</p>

and you can also use meta for this

你也可以为此使用元

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

回答by Quentin

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

But as development tools go, you are probably better off with https://addons.mozilla.org/en-US/firefox/addon/115

但是随着开发工具的发展,使用https://addons.mozilla.org/en-US/firefox/addon/115可能会更好

回答by Cheeso

There's an automatic refresh-on-change tool for IE. It's called ReloadIt, and is available at http://reloadit.codeplex.com. Free.

IE 有一个自动刷新更改工具。它称为 ReloadIt,可从http://reloadit.codeplex.com 获得。自由。

You choose a URL that you'd like to auto-reload, and specify one or more directory paths to monitor for changes. Press F12 to start monitoring.

您选择要自动重新加载的 URL,并指定一个或多个目录路径来监视更改。按 F12 开始监视。

enter image description here

在此处输入图片说明

After you set it, minimize it. Then edit your content files. When you save any change, the page gets reloaded. like this:

设置后,将其最小化。然后编辑您的内容文件。当您保存任何更改时,页面会重新加载。像这样:

enter image description here

在此处输入图片说明

Simple. easy.

简单的。简单。

回答by NarendraSoni

Answer provided by @jAndy should work but in Firefoxyou may face problem, window.location.reload(1); might not work, that's my personal experience.

@jAndy 提供的答案应该有效,但在Firefox 中您可能会遇到问题window.location.reload(1);可能行不通,这是我的个人经验。

So i would like to suggest:

所以我想建议:

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

This is tested and works fine.

这已经过测试并且工作正常。

回答by Dal Hundal

A decent alternative if you're using firefox is the XRefresh plugin. It will reload your page everytime it detect the file has been modified. So rather than just refreshing every 5 seconds, it will just refresh when you hit save in your HTML editor.

如果您使用 Firefox,一个不错的选择是XRefresh 插件。每次检测到文件已被修改时,它都会重新加载您的页面。因此,它不是每 5 秒刷新一次,而是在您在 HTML 编辑器中点击保存时刷新。

回答by veksen

Alternatively there's the application called LiveReload...

或者有一个名为LiveReload的应用程序......

回答by Veera

If you are developing and testing in Firefox, there's a plug-in called "ReloadEvery" is available, which allows you to reload the page at the specified intervals.

如果您在 Firefox 中进行开发和测试,可以使用一个名为“ ReloadEvery”的插件,它允许您以指定的时间间隔重新加载页面。

回答by SeekLoad

This will work on 5 sec.

这将在 5 秒内起作用。

5000 milliseconds = 5 seconds

5000 毫秒 = 5 秒

Use this with target _selfor what ever you want and what ever page you want including itself:

将此与目标_self或您想要的任何内容以及您想要的任何页面一起使用,包括自身:

<script type="text/javascript">
function load()
{
setTimeout("window.open('http://YourPage.com', '_self');", 5000);
}
</script>
<body onload="load()"> 

Or this with automatic self and no target code with what ever page you want, including itself:

或者这个带有自动自我并且没有你想要的任何页面的目标代码,包括它自己:

<script type="text/javascript">
function load()
{
setTimeout("location.href = 'http://YourPage.com';", 5000);
}
</script>
<body onload="load()"> 

Or this if it is the same page to reload itself only and targeted tow hat ever you want:

或者,如果它是同一页面仅重新加载自己并有针对性地选择您想要的帽子:

<script type="text/javascript">
function load()
{
setTimeout("window.open(self.location, '_self');", 5000);
}
</script>
<body onload="load()">

All 3 do similar things, just in different ways.

所有 3 人都做类似的事情,只是方式不同。