How to use setTimeout in Vue.js?

Sometimes, we want to use setTimeout in Vue.js.

In this article, we’ll look at how to use setTimeout in Vue.js.

How to use setTimeout in Vue.js?

To use setTimeout in Vue.js, we just call it directly.

For instance, we write

<script>
export default {
  methods: {
    addToBasket() {
      const item = this.photo;
      this.$http.post("/api/buy/addToBasket", item);
      this.basketAddSuccess = true;
      setTimeout(() => (this.basketAddSuccess = false), 2000);
    },
  },
};
</script>

to call setTimeout in addToBasket to run

this.basketAddSuccess = false

after 2000 milliseconds.

Conclusion

To use setTimeout in Vue.js, we just call it directly.