i'm doing a shopping cart in vue but i'm having some issues. having never used the library i'm probably doing basics wrong, but:
when I add something to the cart I have an onclick function, expander(item) which looks like this:
this.addToCart(item);
this.show = true;
clearTimeout(this.timer);
timer: setTimeout(()=>{ this.show = false; }, 3000);
so what it does it adds to cart, sets the visibility of the cart div to true, then after a delay of three seconds it sets the cart div back to false.
the thing is, the clearTimeout(this.timer) doesn't work. so for each click, after three seconds, no matter what I do it sets the visibility back to false. what I'm trying to do with this function is to reset the timer each time and after reading up it seems like I'm doing it the correct way.
so, I'm guessing my problem is that i need to declare the variable
timer: setTimeout(()=>{ this.show = false; }, 3000);
outside of the function, in order for clearTimeout() to find it at the beginning of my function. my problem here is that wherever I declare it, it can't seem to find it. I've tried declaring the variable in my data {} and also outside of the Vue instance but it doesn't seem to want to find it in my functions.
so: how do I declare the variable? is that even my problem? is there may be an easier fix for this than what it's trying to do?
thanks!