Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!