DA.
DA.

Reputation: 40673

setTimeout giving a 'result of expression is not a function' error

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.

Upvotes: 0

Views: 932

Answers (1)

Pointy
Pointy

Reputation: 413720

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.

Upvotes: 3

Related Questions