Categories
Vue Answers

How to use setTimeout in Vue.js?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *