简体   繁体   中英

html pages auto cicling using js script

I've a servlet that prints html pages. I would like that those pages auto cicling using a js script. So I load an html page, after 3 second it refresh and I load another html page (printed by the same servlet).

In the servlet: - read the session where I store the index of the page i'm printing; - 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

    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

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. So what is the best method to do this?

Use innerHTML

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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