javascript SyntaxError: 在元素列表 [object Object] 之后缺少 ]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20218727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:01:25  来源:igfitidea点击:

SyntaxError: missing ] after element list [object Object]

javascript

提问by Bardelman

I got this error in firebug :

我在 firebug 中遇到了这个错误:

     SyntaxError: missing ] after element list

    [object Object]

for the following javascript piece of code :

对于以下 javascript 代码:

for ( var i = 0; i < 4; i++ ) {
    setTimeout( function(){
        closeBtn( i,'.lt400' );
        // the error exactly happened in next line:
        setTimeout($('#uploaded-holder').hide(), i * 300 );
    }, i * 300 ); 
}

I don't know how a ] can be missing there.. by the way, in chrome i got this error :

我不知道 ] 怎么会在那里丢失..顺便说一下,在 chrome 中我遇到了这个错误:

Uncaught SyntaxError: Unexpected identifier

回答by JAAulde

setTimeoutexpects a function or a string of code as the first parameter. You are passing the result of the evaluation of this expression:

setTimeout需要一个函数或一串代码作为第一个参数。您正在传递此表达式的评估结果:

$('#uploaded-holder').hide()

This expression returns neither a string, nor a function. It returns a jQuery collection.

此表达式既不返回字符串,也不返回函数。它返回一个 jQuery 集合。

You want:

你要:

setTimeout(function () {
    $('#uploaded-holder').hide();
}, i * 300 );

You have an odd set of code there, though, given the combination of setTimeouts and the loop. I would expect some wild oddities to come from it once this error is resolved. For example, iis not going to be what you expect in the execution of many of those internal functions...

但是,考虑到 setTimeouts 和循环的组合,您在那里有一组奇怪的代码。一旦这个错误得到解决,我预计会出现一些疯狂的奇怪现象。例如,i在执行许多这些内部功能时,不会是您所期望的......

回答by Rahul Tripathi

You may try to use this:-

你可以尝试使用这个:-

setTimeout( function () 
{ $('#uploaded-holder').hide() }, i * 300 );

instead of

代替

setTimeout($('#uploaded-holder').hide(), i * 300 );

as setTimeoutexpects a string or a function as first parameter.

因为setTimeout需要一个字符串或一个函数作为第一个参数。

回答by Sumeet Mathew

You can also try this , this also works

你也可以试试这个,这也有效

setTimeout(" $('#uploaded-holder').hide() ", i * 300 );

setTimeout(" $('#uploaded-holder').hide() ", i * 300 );

Add the first parameter within double quotes.

在双引号内添加第一个参数。