代码之家  ›  专栏  ›  技术社区  ›  Ben

setTimeout执行后的Javascript错误“元素列表后缺少]

  •  2
  • Ben  · 技术社区  · 14 年前

    起初我的想法是这是一个语法问题,但我没有看到任何语法问题。我添加了调试代码,结果很奇怪, x 之前已记录 jQuery('#notification')

    document.triggerNotification = function (type, message) {
        jQuery(document.body).append("<div class='push-notification push-"+type+"' id='notification'>"+message+"</div>");
    
        setTimeout(jQuery('#notification').fadeOut(1200, function () {
            console.log(jQuery('#notification'));
            jQuery('#notification').remove();
            console.log(jQuery('#notification'));
        }), 3000);
        console.log('x');
    }
    

    x
    [div#notification.push-notification]
    []
    missing ] after element list - [Break on this error] [object Object]
    

    1 回复  |  直到 14 年前
        1
  •  10
  •   Ryan Kinal    14 年前

    setTimeout

    document.triggerNotification = function (type, message) {
        jQuery(document.body).append("<div class='push-notification push-"+type+"' id='notification'>"+message+"</div>");
    
        setTimeout(function() { jQuery('#notification').fadeOut(1200, function () {
            console.log(jQuery('#notification'));
            jQuery('#notification').remove();
            console.log(jQuery('#notification'));
        })}, 3000);
        console.log('x');
    }
    

    jQuery('#notification').fadeOut() 打电话。以你现在的代码,我希望 fadeOut 立即执行,而不是在指定的3秒钟后执行。