Javascript 使用js为间隔刷新页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12038183/
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
Refresh Page for interval using js
提问by Reddy
How can i refresh a page for every one minute using javascript. Note: I don't have control/option to edit HTML body tag (where we usually call onload function).
如何使用 javascript 每隔一分钟刷新一个页面。注意:我没有编辑 HTML body 标签的控制/选项(我们通常调用 onload 函数)。
回答by Abraham
Just insert this code anywhere in the page:
只需在页面中的任何位置插入此代码:
<script type="text/javascript">
setTimeout(function(){
location = ''
},60000)
</script>
回答by Jo?o Silva
<script type="text/javascript">
setTimeout(function () {
location.reload();
}, 60 * 1000);
</script>
setTimeoutwill reloadthe page after a specified number of milliseconds, hence 60 * 1000 = 1m
. Also, since the page is being refreshed, the timeout will always be set on page load.
setTimeout将在指定的毫秒数后重新加载页面,因此60 * 1000 = 1m
. 此外,由于页面正在刷新,因此将始终在页面加载时设置超时。
回答by Stephen Cioffi
You do not need to have the code in the body tag. Just add this snippet below and it should work no matter where it is in the page.
您不需要在 body 标签中包含代码。只需在下面添加此代码段,无论它在页面中的哪个位置,它都应该可以工作。
<script type="text/javascript">
setInterval('window.location.reload()', 60000);
</script>
As long as you can access the HTML some where and your editor doesn't filter out tags you should be fine. If your editor has a separate area for JavaScript code then just enter setInterval line. :)
只要您可以在某些地方访问 HTML 并且您的编辑器不会过滤掉标签,您就应该没问题。如果您的编辑器有一个单独的 JavaScript 代码区域,那么只需输入 setInterval 行。:)
回答by Amandine Dupays
When your URL has parameters, it seems that using location = ''
doesn't work in IE8. The page reloads without any parameters.
当你的 URL 有参数时,使用location = ''
在 IE8 中似乎不起作用。页面在没有任何参数的情况下重新加载。
The following code works for me :
以下代码对我有用:
<script type="text/javascript">
setTimeout(function(){
window.location.href = window.location.href;
},10000)
</script>