如何停止click事件以连续运行JQuery

时间:2014-04-18 17:12:00

标签: jquery

我想在第二次.click()之后停止点击()运行怎么办?** 我在文档准备好之后将animate()设置为开始3秒,然后使用click()隐藏所有元素,再次使用click()再次显示所有元素,之后如何停止click()运行?

    $(document).ready(function(){

        setTimeout(anim,3000);

        function anim(){
            $('#box1, #box3').animate({width:600+'px'}, 4000, 'easeOutExpo');
            $('#box2').fadeIn(4000);
            $('#btn').animate({left:'+=50px', opacity: '1'}, 2000);
        }

        $('#btn').click(function(){
            $('#box1, #box3').animate({width:0+'px'}, 1000, 'easeOutExpo');
            $('#box2').fadeOut(1000);
                $('#btn').click(function(){
                    $('#box1, #box3').animate({width:600+'px'}, 2000, 'easeOutExpo');
                    $('#box2').fadeIn(2000);
                });
        });

    });

1 个答案:

答案 0 :(得分:1)

不要嵌套活动。

尝试

.one()文档。

function handler1() { //first click code
    $('#box1, #box3').animate({
        width: 0
    }, 1000, 'easeOutExpo');
    $('#box2').fadeOut(1000);
    $(this).one("click", handler2); //set second click code
}
function handler2() { //second click code
    $('#box1, #box3').animate({
        width: 600 + 'px'
    }, 2000, 'easeOutExpo');
    $('#box2').fadeIn(2000);
    //$(this).one("click", handler1); // you can un-comment if you want to loop click1 and click2
};
$('#btn').one('click', handler1);//first click