JQuery restart setInterval

后端 未结 1 965
北荒
北荒 2021-01-16 02:13

Sorry to be a bore but having trouble restarting a setInterval with a toggle function. I get to stop it - when needed, but I cannot get it to restart when the toggle \"clos

1条回答
  •  囚心锁ツ
    2021-01-16 03:11

    Try:

    $('.readmore').live('click',function() {
        $(this).next().slideToggle('slow');
    
        if(!auto_refresh ) {
            auto_refresh = setInterval( function() { $('.holder').load('board.php'); }, 5000 );
        }
        else {
            clearInterval(auto_refresh);
            auto_refresh = null;
        }
    });
    

    Or probably even better, use the state of the element to decide:

    $('.readmore').live('click',function() {
        var $elem = $(this).next();
    
        if( $elem.is(':visible') ) {
            $elem.slideUp('slow');
            clearInterval(auto_refresh);
        }
        else {
            $elem.slideDown('slow');
            auto_refresh = setInterval( function() { $('.holder').load('board.php'); }, 5000 );
    
        }
    });
    

    0 讨论(0)
提交回复
热议问题