简体   繁体   English

使用js脚本的html页面自动冻结

[英]html pages auto cicling using js script

I've a servlet that prints html pages. 我有一个可打印html页面的servlet。 I would like that those pages auto cicling using a js script. 我希望这些页面使用js脚本自动循环。 So I load an html page, after 3 second it refresh and I load another html page (printed by the same servlet). 因此,我加载了一个html页面,三秒钟后刷新,然后加载了另一个html页面(由相同的servlet打印)。

In the servlet: - read the session where I store the index of the page i'm printing; 在servlet中:-阅读会话,在该会话中我存储要打印的页面的索引; - basing on this index, I print the page and I update the index (it increase or it return to 0 if I'm printing last page) - store the index in a div element, so I can find it by javascript function and refresh the html page with a js function -基于此索引,我打印页面并更新索引(如果我要打印最后一页,则索引会增加或返回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>");

The js function find the div element in the html page and update the page 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);   
        }
}

But where have I call the function? 但是我在哪里调用该函数? if I use the 如果我使用

window.onload = refresh();

page load before I stored the index in the div. 在将索引存储在div中之前进行页面加载。 So what is the best method to do this? 那么什么是最好的方法呢?

Use innerHTML 使用innerHTML

var element = document.getElementById("storeDiv").innerHTML;
if (element == '0') {
    //...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM