简体   繁体   中英

How to redirect a page every 10 seconds?

I'm using the following script to refresh my iframe. In case the refresh fails, it will show a different iframe:

<script type=text/javascript>
setInterval(function(){
var rq = new XMLHttpRequest();

rq.open('GET', document.getElementById('tracker').src, true);

rq.onreadystatechange = function() {
    if(rq.readyState === 4) {
        if(rq.status === 200) {
        } else {
    document.write('<iframe src="tracker2.html" style="border:0px #CCD1B9 none;" scrolling="no" frameborder="0" marginheight="0px" marginwidth="0px" height="407px" width="417px"></iframe>');
        }
    }
};
rq.send(null);
    document.getElementById("tracker").src+="";
},2000);
</script>

My problem is below line: } else {

I would like to else, show a different iframe, but to keep reloading the main page again.

So to be clear: the above code is mainpage.html, and it will show iframe tracker2.html if it is unable to refresh iframe tracker.html, and I would like iframe tracker2.html to keep refrshing or REDIRECTING to mainpage.html every X seconds.

It seems like you should combine the logic of mainpage.html and tracker2.html together into one auto refreshing page but you may have your reasons not to do that.

Instead I suggest a meta refresh tag to help auto refresh the page:

<meta content="5; URL=/tracker2.html" http-equiv="Refresh" />

That will refresh after 5 seconds.

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