jquery catch delayed each is finished?

让人想犯罪 __ 提交于 2019-12-11 23:13:23

问题


$("#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


回答1:


Use the .promise() object:

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


来源:https://stackoverflow.com/questions/25708839/jquery-catch-delayed-each-is-finished

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!