setTimeout 给出“表达式的结果不是函数”;错误

发布于 2024-10-20 10:36:37 字数 704 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

郁金香雨 2024-10-27 10:36:37

如果按如下方式更改“resetCard”,会发生什么情况:

function resetCard($card) {
  $($card).removeClass('show');
}

该错误意味着“$card”参数引用的对象上没有“removeClass”属性。

What happens if you change "resetCard" as follows:

function resetCard($card) {
  $($card).removeClass('show');
}

The error means that there's no "removeClass" attribute on the object referenced by the "$card" parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文