setTimeout runs only once?

心不动则不痛 提交于 2019-12-17 19:48:35

问题


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.


回答1:


setTimeout should only run once. You're looking for 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);



回答2:


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

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

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

setInterval(slide, 3000);



回答3:


You are looking for setInterval

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




回答4:


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

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

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

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');



回答5:


Yes, setTimeout only runs once. You need setInterval.




回答6:


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




回答7:


use setTimeout with recursion (endless loop)

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

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



回答8:


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.



来源:https://stackoverflow.com/questions/7867427/settimeout-runs-only-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!