jQuery 如何每 15 秒重新加载一次页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17259787/
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
How can I reload page at every 15 seconds?
提问by subbu
I build one web Application in Wolfram Mathematica,what it does it displays the DB data directly on the browser.
我在 Wolfram Mathematica 中构建了一个 Web 应用程序,它的作用是直接在浏览器上显示 DB 数据。
I placed my Application in Apache Tomcat.
我将我的应用程序放在 Apache Tomcat 中。
I want to reload this page for every 15 seconds,because If you reload the page we will get updated data of DB.
我想每 15 秒重新加载一次此页面,因为如果您重新加载页面,我们将获得数据库的更新数据。
How can I do this?
我怎样才能做到这一点?
回答by Guffa
Add a script like this in the head section of the page:
在页面的 head 部分添加这样的脚本:
<script type="text/javascript">
window.setTimeout(function(){ document.location.reload(true); }, 15000);
</script>
The setTimeout
method will start the callback after 15 seconds, and the reload
method will reload the page. The true
parameter makes the browser always request the page again instead of possibly using a cached version.
该setTimeout
方法将在 15 秒后启动回调,该reload
方法将重新加载页面。该true
参数使浏览器始终再次请求页面,而不是可能使用缓存版本。
回答by krishgopinath
That's bad isn't it? Reloading a page once in 15 secs will break the user experience completely. A user will not be able to even read the page properly to get the context in 15 secs.
这很糟糕不是吗?每 15 秒重新加载一次页面将完全破坏用户体验。用户甚至无法在 15 秒内正确阅读页面以获取上下文。
Why not use ajax
to get results to update the container which contains the data once in 15 secs? By that you'll give the user updated info.
为什么不用于ajax
获取结果以在 15 秒内更新一次包含数据的容器?通过它,您将为用户提供更新的信息。
$.ajax({
type: "POST",
url: "some.php", //some server method which will get u new data from database
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
var i= 0;
for(; i< msg.d.length; i++)
{
$("#container").append(msg.d[i]);
}
});
Where #container
is where you have the data represented.
#container
你在哪里代表数据。
AJAX
Docs : http://api.jquery.com/jQuery.ajax/
AJAX
文档:http: //api.jquery.com/jQuery.ajax/
回答by mhxn ali
Just add <meta content='15' http-equiv='refresh'/>
between <head></head>
. Change the number that you need, like if you need your page to load after 30 secs, then change the value 15 to 30.
只需<meta content='15' http-equiv='refresh'/>
在<head></head>
. 更改您需要的数字,例如如果您需要在 30 秒后加载页面,然后将值 15 更改为 30。
<head>
<meta content='15' http-equiv='refresh'/>
</head>
回答by Christian Berendt
The following code in the head section of an HTML document will also refresh the page without using any JavaScript:
HTML 文档的 head 部分中的以下代码也将在不使用任何 JavaScript 的情况下刷新页面:
<meta http-equiv="refresh" content="15" />