Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda