jQuery重新启动setInterval

jQuery重新启动setInterval

本文介绍了jQuery重新启动setInterval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,但是在使用切换功能重新启动setInterval时遇到麻烦.我可以停止它-需要时,但是在切换开关关闭"时无法重新启动

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 "closes"

这是我的代码

设置setInterval

Set the setInterval

var auto_refresh = setInterval( function() { $('.holder').load('board.php'); }, 5000 );

停止切换-但是希望它在反向切换"上启动

Stop on toggle - BUT want it to start on the "reverse toggle"

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

请多多关照,可能会有所帮助.非常简单,只是我从来不擅长将函数放在函数内"

Help much appreciated, as is prob. very simple just I've never beed good at putting functions "within functions"

推荐答案

尝试:

$('.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 );

    }
});

这篇关于jQuery重新启动setInterval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 15:39