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

164
Visualizações
How to manually stop a watcher?

I need to stop watch() but the docs don't really explain how to do that.

This watcher runs until the loop is finished (1000 seconds):

const state = reactive({
    val: 0
})

watch(() => state.val, () => {
    console.log(state.val)
})

for (let i = 1; i < 1000; i++) {
    setTimeout(function timer() {
        state.val = state.val + 1
    }, i * 1000);
}

How do I stop the watcher after running once? Using watchEffect is not an option because for my use case the watcher needs to run several times before stopped, which is not described in this simplified example. From my understanding watchEffect runs only once (after initiation).

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The "watch" function returns a function which stops the watcher when it is called :

const unwatch = watch(someProperty, () => { });

unwatch(); // It will stop watching

To watch a change only once:

const unwatch = watch(someProperty, () => {
   // Do what your have to do

   unwatch();
});
about 4 years ago · Juan Pablo Isaza 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