Javascript 如果我用slideUp删除一个div,那么计数器将延迟一个数字

Javascript 如果我用slideUp删除一个div,那么计数器将延迟一个数字,javascript,jquery,html,Javascript,Jquery,Html,我正在学习HTML和JS。为了学习,我正在为我的公司编写一个新的票务系统。目前我遇到了一个不知道如何解决的问题 因此,当我在没有slideUp动画的情况下运行脚本时,计数器会正确计数,但当我插入动画时,计数器会延迟一个数字 例如: 4 Notifications shown Counter shows "4" 通过单击一个通知来删除它 3 Notifications shown Counter shows "4" 2 Notifications shown Counter shows "3"

我正在学习HTML和JS。为了学习,我正在为我的公司编写一个新的票务系统。目前我遇到了一个不知道如何解决的问题

因此,当我在没有slideUp动画的情况下运行脚本时,计数器会正确计数,但当我插入动画时,计数器会延迟一个数字

例如:

4 Notifications shown
Counter shows "4"
通过单击一个通知来删除它

3 Notifications shown
Counter shows "4"
2 Notifications shown
Counter shows "3"
通过单击一个通知来删除它

3 Notifications shown
Counter shows "4"
2 Notifications shown
Counter shows "3"
等等

通知删除程序:

$(function(){
$(document).on('click', '.notification_closebtn_img', function(){
    //Del Notification
    $(this).parents(".notification").slideUp('slow', function(){
        $(this).remove();
    });
    //Placeholder
    if ( $("#notification_center").children().length === 0) 
    {
        $( "#notification_center" ).append('<div id="notification_empty"><div class="notification_empty_text"><strong>Keine Benachrichtigungen vorhanden</strong></div></div>');
    }

    set_ticket_counter();
   });
});
有人能帮我吗

托马斯·哈斯这部分代码:

//Placeholder
if ( $("#notification_center").children().length === 0) 
{
    $( "#notification_center" ).append('<div id="notification_empty"><div class="notification_empty_text"><strong>Keine Benachrichtigungen vorhanden</strong></div></div>');
}

set_ticket_counter();
//占位符
if($(“#通知#中心”).children().length==0)
{
$(“#通知中心”)。追加(“Keine Benachrichtigungen vorhanden”);
}
设置票务柜台();
将在动画完成之前执行。所以你的计数器会高一点

将上述代码放置在slideUp回调中,如下所示:

$(function(){
$(document).on('click', '.notification_closebtn_img', function(){
    //Del Notification
    $(this).parents(".notification").slideUp('slow', function(){
        $(this).remove();
        //Placeholder
        if ( $("#notification_center").children().length === 0) 
        {
            $( "#notification_center" ).append('<div id="notification_empty"><div class="notification_empty_text"><strong>Keine Benachrichtigungen vorhanden</strong></div></div>');
        }

        set_ticket_counter();
    });
   });
});
$(函数(){
$(文档)。在('单击','上。通知\u closebtn\u img',函数(){
//删除通知
$(this).parents(“.notification”).slideUp('slow',function()){
$(this.remove();
//占位符
if($(“#通知#中心”).children().length==0)
{
$(“#通知中心”)。追加(“Keine Benachrichtigungen vorhanden”);
}
设置票务柜台();
});
});
});
计数器将等于通知数。

这部分代码:

//Placeholder
if ( $("#notification_center").children().length === 0) 
{
    $( "#notification_center" ).append('<div id="notification_empty"><div class="notification_empty_text"><strong>Keine Benachrichtigungen vorhanden</strong></div></div>');
}

set_ticket_counter();
//占位符
if($(“#通知#中心”).children().length==0)
{
$(“#通知中心”)。追加(“Keine Benachrichtigungen vorhanden”);
}
设置票务柜台();
将在动画完成之前执行。所以你的计数器会高一点

将上述代码放置在slideUp回调中,如下所示:

$(function(){
$(document).on('click', '.notification_closebtn_img', function(){
    //Del Notification
    $(this).parents(".notification").slideUp('slow', function(){
        $(this).remove();
        //Placeholder
        if ( $("#notification_center").children().length === 0) 
        {
            $( "#notification_center" ).append('<div id="notification_empty"><div class="notification_empty_text"><strong>Keine Benachrichtigungen vorhanden</strong></div></div>');
        }

        set_ticket_counter();
    });
   });
});
$(函数(){
$(文档)。在('单击','上。通知\u closebtn\u img',函数(){
//删除通知
$(this).parents(“.notification”).slideUp('slow',function()){
$(this.remove();
//占位符
if($(“#通知#中心”).children().length==0)
{
$(“#通知中心”)。追加(“Keine Benachrichtigungen vorhanden”);
}
设置票务柜台();
});
});
});

计数器将等于通知数。

动画是异步的,在相关完整回调中设置逻辑动画是异步的,在相关完整回调中设置逻辑谢谢:)现在可以正常工作了!谢谢:)现在很好!