Javascript 无用的 setTimeout 调用(参数周围缺少引号?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8211005/
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-08-24 05:08:25  来源:igfitidea点击:

Useless setTimeout call (missing quotes around argument?)

javascriptjquerysettimeoutfirefox3.6

提问by kosta5

I have this sniplet of code in jQuery

我在 jQuery 中有这段代码

$element.parent().children().last().hide().show('slide', {direction : 'left'}, 700, function () {
    $element.delay(2000, function() {
        $element.parent().children().last().hide('slide', {direction: 'left'}, 700);             
        reload_table(question_number);
        //delay ends here
    });
});

delayis declared as:

delay声明为:

jQuery.fn.delay = function(time,func){
    return this.each(function(){
        setTimeout(func,time);
    });
};

Now I get the error:

现在我得到错误:

useless setTimeout call (missing quotes around argument?)

无用的 setTimeout 调用(参数周围缺少引号?)

FF3.x, Firefox 6+ is ok. Any ideas on that? Why could this be happening?

FF3.x, Firefox 6+ 没问题。对此有何想法?为什么会发生这种情况?

采纳答案by Dr.Molle

There already exists a jQuery-method delayand it expects a string(queueName) and not a function as parameter. Choose another name for your delay-method to avoid conflicts.

已经存在一个 jQuery 方法延迟,它需要一个 string(queueName) 而不是一个函数作为参数。为您的延迟方法选择另一个名称以避免冲突。

回答by wsbaser

I got same error when I wrote

我写的时候遇到了同样的错误

setTimeout(updateStatus(), 5000);

instead of

代替

setTimeout(updateStatus, 5000);

回答by mpickell

I agree with wsbaser. I had the additional instance of needed to pass information to the function, and for simplicity used:

我同意 wsbaser。我有一个需要将信息传递给函数的额外实例,为简单起见,使用了:

setTimeout(function(){ updateStatus(myData) } , 5000);

The argument needs to be a function and not a function being called. Firefox caught this error, chrome let it go.

参数必须是一个函数,而不是被调用的函数。Firefox 发现了这个错误,chrome 让它过去了。

回答by polyclick

Just for reference if someone stumbles upon this question and is looking for a possible answer. I got the exact same error message as the initial poster because I was mixing up the setTimeoutarguments order.

如果有人偶然发现这个问题并正在寻找可能的答案,仅供参考。我收到与初始海报完全相同的错误消息,因为我混淆了setTimeout参数顺序。

This:

这个:

setTimeout(25, function(){
    spotlight.intro.find('#intro').ellipsis();  
});

... gave me the error message. I changed the function to this:

...给了我错误信息。我将功能更改为:

setTimeout(function(){
    spotlight.intro.find('#intro').ellipsis();
}, 25);

And my problem was solved.

我的问题解决了。

回答by kosta5

The problem was in Firefox 3. It is not handling some elements properly => Element is completely omitted in the tree and ignored -> hence my original problem

问题出在 Firefox 3 中。它没有正确处理某些元素 => 元素在树中完全省略并被忽略 -> 因此是我原来的问题