简体   繁体   English

Javascript 延迟与 while 循环

[英]Javascript delay with while loop

the site is here I'm trying to create an animated button by moving the position of the background using Javascript when the user clicks the button.该网站在这里我正在尝试通过在用户单击按钮时使用 Javascript 移动背景的 position 来创建一个动画按钮。 However, instead of slowly scrolling, the button jumps to the end of the loop.但是,按钮不会缓慢滚动,而是跳到循环的末尾。 Here's the code:这是代码:

var x=1, y=0, z=1;

function animate () {
    document.getElementById("switch").style.backgroundPosition = y + 'px 0px';
}

function toggle() {
    // check if button is on
    if (x==1) {
        //As long as image is not yet in place, move the background
        while (y>-37) {
            //Delay each move by 500 and increase the delay
            setTimeout("animate()",500*z);
            --y;++z;
        }
        --x;
        //reset increasing timer
        z=1;
    }
    // check if button is off
    else if (x==0) {
        //As long as image is not yet in place, move the background
        while (y<0) {
            //Delay each move by 500 and increase the delay
            setTimeout("animate()",500*z);
            ++y;++z;
        }
        ++x;
        //reset increasing timer
        z=1;
    }
}

Please let me know how to fix.请让我知道如何解决。 Thanks!谢谢!

This works:这有效:

var on = true,
    sw = document.getElementById("switch"),
    stop, y = 0,
    dir, to;

function animate() {
    sw.style.backgroundPosition = y + 'px 0px';
    y += dir;
    if (y != stop) {
        to = setTimeout(animate,25);
    }
}

function toggle() {
    if (to) {
        clearTimeout(to);
    }
    if (on) {
        dir = -1;
        stop = -38;
    }
    else {
        dir = 1;
        stop = 2;

    }
    to = setTimeout(animate, 25);
    on = !on;
}

DEMO演示

Don't know if it is the best way though.不知道这是否是最好的方法。

Note: You either have to run the code in the body.onload event handler or put it at the bottom of the page.注意:您要么必须在body.onload事件处理程序中运行代码,要么将其放在页面底部。

You can also try to play with the step size and the timeout time....您也可以尝试使用步长和超时时间.... there was something else I wanted to say, but I forgot;)我还想说点别的,但我忘记了;)

Another note: You should always use expressive variable names.另一个注意事项:您应该始终使用富有表现力的变量名称。 Eg It was not clear that x is used as a boolean indicator (at least not if you only have a quick lock at it).例如,不清楚x是否用作 boolean 指标(至少如果您只是快速锁定它,则不会)。

The setTimeout function doesn't pause, it just arranges to execute the function (its first argument) after the specified delay and then immediately returns. setTimeout function 不会暂停,它只是安排在指定的延迟后执行 function (它的第一个参数),然后立即返回。 So, launching a whole bunch of timeouts isn't that useful.因此,启动一大堆超时并没有那么有用。 You want the setTimeout callback to call setTimeout to start another timer.您希望setTimeout回调调用setTimeout来启动另一个计时器。 And you can use a function rather than a string as the first argument to setTimeout .您可以使用 function 而不是字符串作为setTimeout的第一个参数。

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

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