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

139
Views
How can a JavaScript class instance clear timers when it is dereferenced?

I have a situation like this:

class Thing {
  constructor() {
    this.timer = setInterval(() => console.log('Hi'), 1000);
  }
}

let a = new Thing();
// ... various stuff happens which takes less than 1s ...
a = undefined;

The problem is, the a instance of Thing continues to trigger the timer closure after a has been set to undefined:

Hi
Hi
Hi
Hi
Hi

Now I could have the parent scope call a method to tell the instance to stop the timer, but the parent doesn't actually know a timer is being used - it's an internal implementation detail of Thing.

How can Thing know it's been dereferenced so it can clean up and allow itself to be GCed?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use a FinalizationRegistry:

class Thing {
  constructor() {
    this.timer = setInterval(() => console.log('Hi'), 1000);
  }
}

let a = new Thing();

const registry = new FinalizationRegistry(timer => {
    clearInterval(timer);
    console.log('Cleared');
});

registry.register(a, a.timer);

a = undefined;

Note that it might take some time before the object is garbage collected. In my tests of the above code, the interval ran about 5-20 times before being eventually cleared.

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!