jQuery catch延迟每个完成吗? [英] jquery catch delayed each is finished?

查看:132
本文介绍了jQuery catch延迟每个完成吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$("#div_game_container").on("click", ".square", squareClick);

function squareClick() {
    var group = $(this).data("group");

    if ($("." + group).hasClass("active")) {

        // off click event
        $("#div_game_container").off("click", ".square", squareClick);

        $("." + group).each(function (index) {
            $(this).delay(100 * index).fadeOut(100);
        });

        // on click event. this line does not wait "each" function 
        $("#div_game_container").on("click", ".square", squareClick);
}

以上代码不起作用.实际上,它可以正常工作,但不是我所期望的.我注释出了什么问题.

above code does not work. Actually, it works but not my expected. I commented out what is the problem.

我还添加了这样的延迟:

Also I add the delay like this:

$("#div_game_container").delay(each_delay_time).on("click", ".square", squareClick);

但没有变化.如何捕捉到每个"迭代已完成?我想在每个方法运行时以及每个方法完成后防止单击事件,请防止发生单击事件

but there is no change. How can I catch that "each" iteration is finished? I want to prevent click event, while each method is running, and after each method finished, then active click event

推荐答案

使用 .promise() 对象:

$("." + group).each(function (index) {
    $(this).delay(100 * index).fadeOut(100);
});
$("." + group).promise().done(function() {
    $("#div_game_container").on("click", ".square", squareClick);
});

这篇关于jQuery catch延迟每个完成吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆