【发布时间】:2018-02-16 11:22:56
【问题描述】:
我有一个打印 html 页面的 servlet。我希望这些页面使用 js 脚本自动循环。所以我加载了一个 html 页面,3 秒后它刷新,然后我加载了另一个 html 页面(由同一个 servlet 打印)。
在 servlet 中: - 阅读我存储正在打印的页面索引的会话; - 基于这个索引,我打印页面并更新索引(如果我打印最后一页,它会增加或返回到 0) - 将索引存储在 div 元素中,这样我就可以通过 javascript 函数找到它并使用 js 函数刷新 html 页面
out.println("<div id=\"storedDiv\">");
int indexRefresh= (int) request.getSession().getAttribute("indexRefresh");
out.println(Integer.toString(indexRefresh) );
indexRefresh=indexRefresh+1;
if (indexRefresh==Tot) {
indexRefresh=0;
}
request.getSession().setAttribute("indexRefresh", indexRefresh);
out.println("</div>");
js函数在html页面中找到div元素并更新页面
function refresh(){
var element = document.getElementById("storeDiv");
if (element==0){
setTimeout(function() {
window.location.href = "http://localhost:9080/HydroGui/Query?anno=2018&id=1";
}, 3000);
}
if (element==1){
setTimeout(function() {
window.location.href = "http://localhost:9080/HydroGui/Query?anno=2018&id=2";
}, 3000);
}
}
但是我在哪里调用函数呢?如果我使用
window.onload = refresh();
在我将索引存储在 div 中之前加载页面。 那么最好的方法是什么?
【问题讨论】:
标签: javascript html refresh