Javascript clearTimeout 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5397117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:01:11  来源:igfitidea点击:

clearTimeout not working

javascriptjquery

提问by Lucas Veiga

Hey guys, im building a simple banner rotator. The fact is, when it rotates without any buton pressed, works fine, but when i press some button to change the banner and clear the time, its doesnt work.

嘿伙计们,我正在构建一个简单的横幅旋转器。事实是,当它在没有按下任何按钮的情况下旋转时,工作正常,但是当我按下某个按钮来更改横幅并清除时间时,它不起作用。

Looks like the time does not clear.

时间好像不清楚。

Thanks!

谢谢!

        var tempo = 5000;
        var elemento;
        var quantos;
        var atual;

        // Inicia

        $(document).ready(function() {
        ? ? bannerRotator("#destaques");

        });


        // Fun??es do Banner


        function bannerRotator(element) {

            // Conta quantos banners existem:
            $('<ul class="buttons"></ul>').appendTo(element);
            i = 0;
            $(element).find(".banner").each(function() {
                $(element).find(".banner").eq(i).addClass("id"+i);
                buttons = element+" ul.buttons";
                acId = i+1;
                $('<li><a href="javascript:getBanner('+i+');">'+acId+'</a></li>').appendTo(buttons);
                i++;
            });

            // Inicia a rotacao
            elemento = element;
            quantos = i;
            rotate(i,-1);

        }

        function getBanner(r) {
            r = r-1;
            rotate(quantos, r);
        }


        function rotate(i, base) {

            clearTimeout(tempo);

            if (base<i-1) {
                base++;
                atual = base;
                setTimeout('rotate('+i+', '+base+');', tempo);
            }
            else {
                base = 0;
                atual = base;
                setTimeout('rotate('+i+', '+base+');', tempo);
            }

            // Faz os fades

            $(elemento).find(".banner").animate({opacity: 0,});
            $(elemento).find(".banner").eq(base).animate({opacity: 1,});

            // Arruma os botoes

            $(elemento).find("ul.buttons li").removeClass("active");
            $(elemento).find("ul.buttons li").eq(base).addClass("active");

        }

回答by Travis Webb

Because you're using clearTimeout()incorrectly. Your code needs to resemble the following:

因为你用clearTimeout()错了。您的代码需要类似于以下内容:

var x = setTimeout("doStuff();", tempo);
clearTimeout(x);

You are currently using tempoas the timeout handle, which is why it isn't working.

您当前tempo用作超时句柄,这就是它不起作用的原因。

回答by capi

Use the return from setTimeoutto pass it to the clearTimeoutfunction :

使用 return fromsetTimeout将其传递给clearTimeout函数:

var timeoutId = setTimeout(callBack, 1000);
//then, later in the code
clearTimeout(timeoutId);

回答by Rocket Hazmat

To use clearTimeoutyou need to pass it the value returned from a call to setTimeout.

要使用,clearTimeout您需要将调用返回的值传递给setTimeout.

var timeout;
// ...
timeout = setTimeout('rotate('+i+', '+base+');', tempo);
// ...
clearTimeout(timeout);

回答by mothmonsterman

You clear the timeout with the result returned from setTimeout...

您使用从 setTimeout 返回的结果清除超时...

var x = setTimeout(functionPointer, 500);
clearTimeout(x);

回答by Zikes

When you call setTimeout it will return an identifier. That is what needs passed to clearTimeout.

当您调用 setTimeout 时,它将返回一个标识符。这就是需要传递给 clearTimeout 的内容。

回答by Naftali aka Neal

you never set tempoas a timer! do:

你从来没有设置tempo为计时器!做:

tempo = setTimeout('rotate('+i+', '+base+');', timeOutTime);

Then set timeOutTimeas the number of ms you want the timer to go to

然后设置timeOutTime为您希望计时器转到的毫秒数