setTimeout 给出“表达式的结果不是函数”;错误
我有一个 setTimeout 调用:
if ($cardOne instanceof jQuery){
setTimeout(function() {
resetCard($cardOne);
}, 1000);
$cardOne = "";
}
调用此函数:
function resetCard($card){
$card.removeClass('show');
}
这会导致此错误:
表达式 '$card.removeClass' [undefined] 的结果不是函数。
我不确定这意味着什么。 setTimeOut 是否想要某种返回值?我已经验证 $card 确实是一个 jQuery 对象(在本例中是一个 DIV)。
更新:
我在上面添加了一些示例代码来指出我做错了什么。 Pointy 让我意识到问题是当调用removeClass 时$card 不是一个jQuery 对象。
如果你看看我的示例代码,事后看来我做错了什么很明显......我在 setTimeout 中调用该函数,然后立即将 $cardOne var 设置回空字符串。因此,当 setTimeout 调用该函数时,var 已被重置并且不再是 jQuery 对象。
修复方法是将对象的设置移至 ResetCard 函数中的空字符串。
I have a setTimeout call:
if ($cardOne instanceof jQuery){
setTimeout(function() {
resetCard($cardOne);
}, 1000);
$cardOne = "";
}
calling this function:
function resetCard($card){
$card.removeClass('show');
}
This results in this error:
Result of expression '$card.removeClass' [undefined] is not a function.
And I am not sure what that means. Is setTimeOut wanting a return value of some sorts? I have verified that $card is, indeed, a jQuery object (in this case a DIV) .
UPDATE:
I added some more example code above to point out what I was doing wrong. Pointy got me to realize that the issue was that $card was not a jQuery object when the removeClass was being called on it.
If you look at my sample code, it's now obvious in hindsight what I was doing wrong...I was calling the function inside a setTimeout and then immediately setting the $cardOne var back into an empty string. So, by the time setTimeout called the function, the var had been reset and no longer a jQuery object.
The fix is to move the setting of the object to an empty string into the resetCard function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果按如下方式更改“resetCard”,会发生什么情况:
该错误意味着“$card”参数引用的对象上没有“removeClass”属性。
What happens if you change "resetCard" as follows:
The error means that there's no "removeClass" attribute on the object referenced by the "$card" parameter.