0

我有两个 JS 函数:一个显示进度条的 load() 函数和一个在页面加载后停止执行加载的 kill() 函数。现在,当加载另一个页面时,不会显示进度条,因为知道在每个页面上都会调用 load 函数。关于问题可能出在哪里以及是否有解决方法的任何提示。

这是我的代码:

<script type="text/javascript">
    var count=0;
    function load(i) {
        j = parseInt(i);
        document.getElementById("progressBar").style.display = "block";
        count=count+1;
        if (document.all) { 
            document.all.btn1.value=count+'%';
            document.all.progressbar.pic1.width=2*count;
        }
        else { 
            document.getElementById("pic1").width=2*count;
            document.getElementById("bar").width=count+'%';
        } 
        if (count<100) {
            setTimeout('load(j)',j);
        }
        if(count==100) { 
            document.getElementById("progressBar").style.display = "none";
            count=0;
        }
    }

    function kill(){
        if (document.applets[0].isActive()) {
            document.getElementById("progressBar").style.visibility = "hidden"; 
        } 
    }

</script>

先感谢您 !

4

2 回答 2

1

load()您将显示更改为block,但在kill()您将可见性设置为hidden; 您应该将显示设置为,以便下次none可以再次正确设置。阅读有关可见性的信息。block

优化代码:

<script type="text/javascript">
    var count = 0,
        win = window,
        doc = document,
        progressBar = doc.getElementById("progressBar"),
        t, j;

    function load(i) {
        j = parseInt(i);
        progressBar.style.display = "block";
        count++;

        // no actual need to check if doc.all is available
        // just select through id
        doc.getElementById("pic1").style.width = 2*count;
        doc.getElementById("bar").style.width = count+'%'; 

        if (count < 100) {
            t = win.setTimeout('load(j)',j);
        } else { 
            progressBar.style.display = "none";
            win.clearTimeout(t);
            count = 0;
        }
    }

    function kill(){
        if (doc.applets[0].isActive()) {
            progressBar.style.display = "none";
        } 
    }

</script>
于 2013-04-15T09:50:27.853 回答
0

如果你分配setTimeout给一个变量,你可以在它上面使用 clearTimeout 来停止它。
例如设置超时, t = setTimeout('load(j)',j); 然后停止它 clearTimeout(t);让我知道这是否有帮助,并且有意义:)

于 2013-04-15T09:53:15.413 回答