setTimeout runs only once?

前端 未结 8 1622
醉酒成梦
醉酒成梦 2020-12-10 10:43
function slide()
{
    if($(\'.current\').is(\':last-child\')){
        $(\'.current\').removeClass(\'.current\');
        $(\'#imgholder\').first().addClass(\'.curr         


        
8条回答
  •  时光说笑
    2020-12-10 11:04

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

提交回复
热议问题