每30秒自动刷新页面 [英] Auto refresh page every 30 seconds

查看:314
本文介绍了每30秒自动刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSP页面,该页面必须显示正在运行的各种作业的状态.其中一些作业需要时间,因此其状态从处理更改为完成需要一段时间.

I have a JSP page which has to display the status of various jobs that are running. Some of these jobs take time, so it takes a while for their status to change from processing to complete.

拥有一个每30秒左右刷新一次页面的javascript函数是一个好主意吗?具有不断刷新页面的脚本有什么影响?

Is it a good idea to have a javascript function that would refresh the page every 30 seconds or so? Are there any ramifications for having a script that is constantly refreshing a page?

另一种选择是具有一个刷新按钮,单击该按钮可以刷新页面.

The other option is to have a refresh button which on click would refresh the page.

推荐答案

对此有多种解决方案.如果您希望刷新页面,实际上不需要JavaScript,那么只要在head标签中添加此meta标签,浏览器便可以为您完成操作.

There are multiple solutions for this. If you want the page to be refreshed you actually don't need JavaScript, the browser can do it for you if you add this meta tag in your head tag.

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

然后,浏览器将每30秒刷新一次页面.

The browser will then refresh the page every 30 seconds.

如果您确实想使用JavaScript进行操作,则可以使用location.reload()(文档):

If you really want to do it with JavaScript, then you can refresh the page every 30 seconds with location.reload() (docs) inside a setTimeout():

setTimeout(function() {
  location.reload();
}, 30000);

否则,如果您不需要刷新整个页面而只刷新其中一部分,我想Ajax调用将是最有效的方法.

Otherwise, if you don't need to refresh the whole page but only a part of it, I guess an Ajax call would be the most efficient way.

这篇关于每30秒自动刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆