Javascript Javascript函数每X秒重新加载一个页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3992353/
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 07:03:40  来源:igfitidea点击:

Javascript function to reload a page every X seconds?

javascripthtmlsyntax

提问by Tom

A couple of questions:

几个问题:

  • I've never really used JS listeners other than onclickand onkeyevents, so I wondered if someone could help me with what I need in order to reload the page every X seconds?

  • Secondly, the page contains bare minimum, literally just one input box. Do I still need to include the html headand body?

  • 除了onclickonkey事件之外,我从未真正使用过 JS 侦听器,所以我想知道是否有人可以帮助我解决我需要的每 X 秒重新加载页面的问题?

  • 其次,页面包含最少的内容,实际上只有一个输入框。我还需要包含 htmlheadbody吗?

回答by Greg Hewgill

You don't need Javascript for this simple function. Add in the page header:

这个简单的功能不需要 Javascript。在页眉中添加:

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

300 is the number of seconds in this example.

300 是本例中的秒数。

回答by pkaeding

To reload the page after 5 seconds (5000 milliseconds) using JavaScript, add the following to the bottom of the page:

要使用 JavaScript 在 5 秒(5000 毫秒)后重新加载页面,请将以下内容添加到页面底部:

<script type="text/javascript">
  setTimeout(function () { location.reload(true); }, 5000);
</script>

As Greg Hewgill notes, you can also accomplish this with the meta refresh tag:

正如 Greg Hewgill 所指出的,您还可以使用元刷新标签来完成此操作:

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

Strictly speaking, you do still need <html>and <body>tags. Some browsers may render the page correctly without them, but your are safest to include them.

严格来说,你仍然需要<html><body>标签。某些浏览器可能会在没有它们的情况下正确呈现页面,但包含它们是最安全的。

回答by Bart

use a timer: http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/and usee ajax to reload if it is dynamic

使用计时器:http: //www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/ 并使用 ajax 重新加载,如果它是动态的

回答by Trufa

To your second question, ye you definitely do!!

对于你的第二个问题,你肯定知道!!

Toy your first question check this out:

玩弄你的第一个问题,看看这个:

http://www.javascriptkit.com/script/script2/autofresh.shtml

http://www.javascriptkit.com/script/script2/autofresh.shtml

Or this to do it without javascript:

或者在没有 javascript 的情况下做到这一点:

http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm

http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm

It does what you asked and is very easy to configure!

它可以满足您的要求并且非常容易配置!