Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
How to delay initial button event in Vue 3 and JavaScript

I am attempting to set up a button event in Vue 3 that displays a setTimeout countdown on the button before routing to another page. My event function includes a conditional that executes a countdown from 5 to 0 when the countValue is > 0. The setTimeout function is recursive in that it calls the goHome() function each time to execute a countdown. When the countValue is 0, then router.push is called to re-route to another page. My issue is that when I press the button, the countdown immediately lumps from 5 to 4. I am wanting to briefly delay the countdown when I first push the button. In other words, I do not want the countdown to immediately jump to 4 when I push the button. How can I fix this?

Here is my code so far:

Template:

<div>
  <button @click="goHome" type="button" :disabled="disabledOnCount === true ? true : false">Go Home in {{ countValue }} seconds</button>
</div>

Script:

import { ref } from '@vue/reactivity'

const countValue = ref(5)

const goHome = () => {
  if (countValue.value > 0) {
    disabledOnCount.value = true
    countValue.value -= 1
    setTimeout(() => {
      goHome()
      if (countValue.value === 0) {
        router.push({ name: 'home' })
      }
    }, 1000)
  }
}
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The countdown jumps to 4 immediately because goHome() immediately decrements the value, and then calls setTimeout() to check the value. Changing the countdown value and reading it should happen in the same tick:

const goHome = () => {
  if (countValue.value > 0) {
    disabledOnCount.value = true
    countValue.value -= 1 // ❌ decremented immediately
    setTimeout(() => {
      goHome()
      if (countValue.value === 0) {
        router.push({ name: 'home' })
      }
    }, 1000)
  }
}

Solution

A few changes:

  1. goHome() should always call setTimeout() so that its effect is always delayed.

  2. The timer callback should only call goHome() when the countdown is nonzero. Otherwise, the callback has no effect, so no need to call it.

const goHome = () => {
  disabledOnCount.value = true

  1️⃣
  setTimeout(() => {
    if (countValue.value > 0) {
      countValue.value -= 1
      if (countValue.value === 0) {
        router.push({ name: 'home' })
      } else {
        2️⃣
        goHome()
      }
    }
  }, 1000)
}

If it's possible for the page to be unmounted before the timer finishes (e.g., when the user changes the page), the timer should be stopped on unmount to free resources. Otherwise, the timer can continue running while the user has navigated away, and then suddenly, they're pushed to the home page unexpectedly.

To stop the timer, you have to track the timer ID, and then use the onUnmounted hook to clearTimeout() on that ID:

import { onUnmounted } from 'vue'

let countdownTimerId = null

const goHome = () => {
  countdownTimerId = setTimeout(() => {
    /* same as above */

    if (countValue.value === 0) {
      countdownTimerId = null
    }
  }, 1000)
}

onUnmounted(() => clearTimeout(countdownTimerId))

demo

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda