Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9182185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:30:02+00:00 2026-06-17T18:30:02+00:00

Im wondering how to properly Clear a object instance. With the code below, the

  • 0

Im wondering how to properly “Clear” a object instance.
With the code below, the internal setInterval() will continue to run even after the instance is “cleared” by the parent.

// simple Class
var aClass = function(){
    return {
        init: function(){
            console.log("aClass init()")
            setInterval( this.tick, 1000 );
            // note: we're not storing the ref
        },
        tick: function(){
            console.log("aClass tick");
        }
    }
}

// instantiate the class
var inst = new aClass();
inst.init();


// try to forget the instance
function test(){
    console.log("test() 1 inst:", inst);

    inst = null;

    console.log("test() 2 inst:", inst);
}

// run for a while, then call test()
setTimeout( test, 4000 );

Output:

aClass init()
aClass tick
aClass tick
aClass tick
test() 1 inst: {.....}
test() 2 inst: null
aClass tick
aClass tick ...

Problem is that the “aClass tick” message continues to print after the test().

Ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:30:03+00:00Added an answer on June 17, 2026 at 6:30 pm

    Your instance is being kept in memory because the function you passed to setInterval is being kept in memory and has a reference to it. That function is referenced by the browser’s list of active interval timers.

    You’ll need to remember the interval handle:

    this.intervalHandle = setInterval( this.tick, 1000 );
    

    …then later when you’re dropping your reference to inst, you’ll want to tell it that:

    inst.cleanup();
    inst = null;
    

    …and in inst.cleanup:

    clearInterval(this.intervalHandle);
    

    That way, the browser will remove its reference to the function you passed to setInterval, which means that function will become eligible for garbage collection (based on your code there are no other references to it). And that means the reference it has to your instance is released, and so if no other outstanding references to that instance exist, it’s eligible for garbage collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering about the .childNodes property, I have the code below, and for some
While going through and properly documenting old code, I was wondering, what standards are
I'm programming an iPhone app and I'm wondering how programmers properly position controls without
I am wondering what is the best way to properly create a Development and
I'm not sure if this question fits properly, but I'm wondering in tile-based games,
I'm wondering how to correctly use the IconFile property of a TabInfo object in
I am wondering how I can programmatically update a Core Data object. The object
I've properly integrated Braintree with my Rails 3.1 application, I'm just wondering what's the
I am wondering how to properly lock the getter of type List<String> . I
I'm wondering if I am understanding the concepts of requestCode and resultCode properly? Basically,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.