I am currently working on implementing a shopping cart using Vue, but I am encountering some challenges. As I am new to using this library, it is possible that I am making some basic mistakes. Here is the issue I am facing:
When I add an item to the cart, I have an onclick
function called expander(item)
that performs the following actions:
this.addToCart(item);
this.show = true;
clearTimeout(this.timer);
timer: setTimeout(()=>{ this.show = false; }, 3000);
Essentially, this function adds the item to the cart, makes the cart div visible, and then after a delay of three seconds, hides the cart div again.
However, the issue I am facing is that the clearTimeout(this.timer)
does not seem to work. This means that regardless of my actions, after three seconds, the visibility of the cart div always reverts to false. From my research, it appears that I am implementing the timer reset correctly in the function.
My assumption is that I may need to declare the variable
timer: setTimeout(()=>{ this.show = false; }, 3000);
outside of the function in order for clearTimeout()
to locate it at the beginning of my function. However, no matter where I declare it - whether in my data {}
or outside the Vue instance - it does not seem to be recognized within my functions.
Therefore, my question is: how should I declare the variable? Is this even the root of my problem? Are there simpler solutions to this issue than what I am currently attempting to implement?
Thank you in advance!