你如何使用javascript多次重复一个过程

时间:2012-01-13 14:18:37

标签: javascript

我找到了以下代码,我想知道如何在更改newPage之前重复此代码9次。我现在正在做的是制作10个html文档,我将newPage更改为page2.html,page3.html,page4.html,因此在完成计数后,它会更改并最终遍历所有这些html文档。我想保留它只有2个文件。 index.html使用此代码和end.html。 index ill执行此代码9次然后更改为end.html。有人可以帮忙吗?

var startTime = 45;
var newPage = "page2.html";

function countDown() {
    startTime--;
    document.getElementById("counter_display").innerHTML = startTime;

    if (startTime == 0) {
        window.location = newPage;
    }
}

function gett(id) {
    if (document.getElementById) {
        return document.getElementById(id);
    }
    if (document.all) {
        return document.all.id;
    }
    if (document.layers) {
        return document.layers.id;
    }
    if (window.opera) {
        return window.opera.id;
    }
}

function watchNow() {
    if (gett('counter_display')) {
        setInterval(countDown, 1000);
        gett("counter_display").innerHTML = startTime;
    } else {
        setTimeout(watchNow, 50);
    }
}

document.onload = watchNow();

<p><b id="counter_display"></b></p>
<iframe frameborder="no" height="735" src="http://website.com/video.php" width="385"></iframe>

1 个答案:

答案 0 :(得分:1)

如果您使用AJAX将其他页面的内容加载到框架中的当前页面,那么您可以将最后一行替换为:

document.onload = function () {
    for (var i= 1; i < 10; i++) {
       newPage = "page"+i+".html";
       watchNow();
    }
}

但至于你如何做第一点,我需要更多关于这些页面的内容的信息等。