简体   繁体   English

JavaScript中的window.settimeout加载速度太快

[英]window.settimeout in javascript loading too fast

I have this js code that has to make the height of a panel bigger on load of my page. 我有这个js代码,必须使页面加载时面板的高度更大。 But it seems to be loading it way too fast. 但它似乎加载得太快了。

var TimerID;

function LoadDoc() {
    for(i=0;i<=100;i++){    
        TimerID=window.setTimeout(MoveRolldownDown(i),5000);
    }
}

function MoveRolldownDown(i){
    document.getElementById('Rolldown').style.height=i + '%';
    window.clearTimeout(TimerID);
}

This loads in the page nearly instantly, so how can i make this load slower. 这几乎立即加载到页面中,因此如何降低加载速度。 At the top of my HTML page i have this code 在我的HTML页面顶部,我有以下代码

document.onreadystatechange = function () {
    if(document.readyState === "complete"){
        LoadDoc();
    }
}  

1st thing -- your functions are executing immediately so you need to put them inside of another function. 第一件事-您的函数正在立即执行,因此您需要将它们放在另一个函数中。

One more thing -- all of your timeouts end at basically the same time! 还有一件事-您的所有超时基本上都在同一时间结束!

Try something like this: 尝试这样的事情:

function LoadDoc() {
    for (i = 0; i <= 100; i++) {
        var down = i;
        setTimeout((function (down) {
            return function(){ //return function for timeout
                MoveRolldownDown(down);
            };
        })(i), 10 * i);
    }
}

function MoveRolldownDown(i) {
    document.getElementById('Rolldown').style.height = i + '%';
}

Demo: http://jsfiddle.net/maniator/RTaZh/ 演示: http//jsfiddle.net/maniator/RTaZh/

Yes, it because you are calling the function within the parameters, 是的,这是因为您正在参数中调用函数,

You probably want something like 您可能想要类似

window.setTimeout(MoveRolldownDown,5000, [i]);

https://developer.mozilla.org/en-US/docs/Web/API/Window.setTimeout https://developer.mozilla.org/en-US/docs/Web/API/Window.setTimeout

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

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