How do I create a simple 10 seconds countdown in Vue.js

I want to do a simple countdown from 10 to 0

I found solution online using normal javascript but let say I want to do it in Vue . The solution in Jquery

Create a simple 10 second countdown

<template>
   {{ countDown }}

</template>

<script>
  export default {
    computed: {
       countDown() {
         // How do i do the simple countdown here?
       }

    }

  }

</script>

How do I recreate the same functionality in Vue.js?

Thanks

Answer

Please check if this works for you.

<template>
   {{ countDown }}
</template>

<script>
    export default {
        data() {
            return {
                countDown : 10
            }
        },
        method: {
            countDownTimer() {
                if(this.countDown > 0) {
                    setTimeout(() => {
                        this.countDown -= 1
                        this.countDownTimer()
                    }, 1000)
                }
            }
        }
        created: {
           this.countDownTimer()
        }
    }
</script>

Leave a Reply

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