首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >执行setTimeout后,在元素列表之后出现Javascript错误"missing ]

执行setTimeout后,在元素列表之后出现Javascript错误"missing ]
EN

Stack Overflow用户
提问于 2010-09-21 22:22:18
回答 1查看 4.2K关注 0票数 2

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

代码语言:javascript
复制
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');
}

Firebug提供以下输出:

代码语言:javascript
复制
x
[div#notification.push-notification]
[]
missing ] after element list - [Break on this error] [object Object]

一切都在成功执行,但仍然抛出错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-09-21 22:27:32

setTimeout需要一个函数作为它的第一个参数。您为它提供了一个jQuery对象的集合。尝试以下操作:

代码语言:javascript
复制
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秒之后执行。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3761253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档