Javascript setTimeout 只运行一次?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7867427/
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-24 03:54:25  来源:igfitidea点击:

setTimeout runs only once?

javascriptsettimeout

提问by pahnin

function slide()
{
    if($('.current').is(':last-child')){
        $('.current').removeClass('.current');
        $('#imgholder').first().addClass('.current');
        $('#imgholder').animate({left: '3920px'});
    }
    else{
        $nxt=$(".current");
        $(".current").removeClass("current");
        $nxt.next().addClass("current");
        $('#imgholder').animate({left: '-=980'},{duration: 'slow', easing: 'easeOutBounce' });
        }
}
var loop_handle= setTimeout("slide()",'3000');

I have put this code in header section and the setTimeout runs only once.

我已将此代码放在标题部分中,并且 setTimeout 仅运行一次。

回答by Rob W

setTimeoutshould only run once. You're looking for setInterval.

setTimeout应该只运行一次。您正在寻找setInterval.

var loop_handle = setInterval(slide, 3000);

Also, the second argument should be a number, not a string. When the function call doesn't require any arguments, it's better to reference to the function instead of using a string. A string would be converted to a function. This function will be executed within the scope of the window.

此外,第二个参数应该是一个数字,而不是一个字符串。当函数调用不需要任何参数时,最好引用函数而不是使用字符串。一个字符串将被转换为一个函数。该函数将在窗口范围内执行。

  setInterval("slide()", 3000);
//becomes
  setInterval(Function("slide();"), 3000);

回答by Pointy

You only call it once, so it'll only execute once.

你只调用一次,所以它只会执行一次。

Perhaps you're thinking of "setInterval()".

也许您正在考虑“setInterval()”。

When you call it, by the way, just pass the name of the function and not a string:

顺便说一下,当您调用它时,只需传递函数的名称而不是字符串:

setInterval(slide, 3000);

回答by AndreyKo

You are looking for setInterval

您正在寻找 setInterval

See: https://developer.mozilla.org/en/window.setInterval

请参阅:https: //developer.mozilla.org/en/window.setInterval

回答by ravy amiry

The setTimeoutfunction only runs once! If you want to run it in more times you should use setInterval:

setTimeout功能只运行一次!如果你想多次运行它,你应该使用setInterval

var loop_handle= setInterval("slide()",'3000');

Also you can use setTimeoutin the end of the slide()function to re-set-timeout again:

您也可以setTimeoutslide()函数的末尾再次使用重新设置超时:

var loop_handle;
function slide() {
    if($('.current').is(':last-child')) {
        $('.current').removeClass('.current');
        $('#imgholder').first().addClass('.current');
        $('#imgholder').animate({left: '3920px'});
    }
    else {
        $nxt=$(".current");
        $(".current").removeClass("current");
        $nxt.next().addClass("current");
        $('#imgholder').animate({left: '-=980'},{duration: 'slow', easing: 'easeOutBounce' });
    }
    loop_handle = setTimeout("slide()",'3000');
}
loop_handle = setTimeout("slide()",'3000');

回答by DonVaughn

Yes, setTimeout only runs once. You need setInterval.

是的,setTimeout 只运行一次。你需要 setInterval。

回答by MeLight

That's because setTimeout()is supposed to run only once. In order to fire an event on set intervals user setInterval().

那是因为setTimeout()应该只运行一次。为了按设置的时间间隔触发事件,用户setInterval()

回答by DevWL

use setTimeout with recursion (endless loop)

将 setTimeout 与递归一起使用(无限循环)

If you want to keep the exact space between each function call use setTimeoutinstead setInterval. setIntervalcan overlap at some point and this is often not expected behavior.

如果您想在每个函数调用之间保持精确的空间,请使用setTimeout而不是 setInterval。setInterval可以在某些时候重叠,这通常不是预期的行为。

(function test(){
  setTimeout(function(){
    console.log(1);
    testt();
  }, 2000)
})()

回答by Cyber

This problem usually happens when you used setTimeout in recursion loop, for example in ajax callback and when you want to run something out of recursion, you expect setTimeout to work as past. remember to use setInterval in non-recursion functions.

当您在递归循环中使用 setTimeout 时,通常会发生此问题,例如在 ajax 回调中,并且当您想从递归中运行某些内容时,您希望 setTimeout 像过去一样工作。记住在非递归函数中使用 setInterval 。