0

I have a problem with timers. I know that if I set obj = nul; or delete object delete obj; it will removed from memory. But it not works if I have interval in my object. delete and obj = null; not working. How to delete a timers without doing this in the deleted object.

I think Angular4 clear only those timers from component that will be removed. Background timers not remove.

function MyFunc() {
  setInterval(function () {
    console.log('Interval');
  }, 1000);
}

var obj = new(MyFunc.bind({
  message: 'some_message'
}));

// From some time remove object
setTimeout(function() {
  delete obj;
  console.log('Delete object');
}, 5000);

Sorry for english.


  • `clearInterval(sth)` maybe? Please show your code. And note that you dont have access to intervals at all. They are somewhere deep in memory regions that you cant access. You can however clear them through passig the inrerval id to clearInterval. – Jonas Wilms Feb 20 '18 at 16:31
  • @JonasW., Updated. I use something like this. I also checked Angular project with interval timers and saw, that when I leave page where I started timer, timer not removed. Can you suggest me, how check that I remove object from memory `obj = null;` or `delete obj`? Thanks. – Станислав Пишевский Feb 20 '18 at 16:48
  • Possible duplicate of [Stop setInterval call in JavaScript](https://stackoverflow.com/questions/109086/stop-setinterval-call-in-javascript) – Cory Danielson Feb 20 '18 at 16:49
  • 1
    What are you actually trying to achieve here? That bound constructor looks a bit suspicious :) – Jonas Wilms Feb 20 '18 at 16:51
  • 1
    First you [can't unset variable](https://stackoverflow.com/questions/1596782/how-to-unset-a-javascript-variable) `let deleted = delete obj;` will be false. Also spec doesn't define anything about GC semantic so one can't define any destructor logic to run automatically. – Yury Tarabanko Feb 20 '18 at 17:11
  • @JonasW. My target was to know it possible automaticaly remove timers that was run from specific object – Станислав Пишевский Feb 20 '18 at 17:42
  • 2
    `new(MyFunc.bind(…))` - WTF? – Bergi Feb 20 '18 at 18:35
  • @Bergi, =) It is for for binding object. – Станислав Пишевский Feb 20 '18 at 20:08
  • @СтаниславПишевский Except that it doesn't. The `obj` is empty and not used for anything, and the object literal with the message is outright ignored. – Bergi Feb 20 '18 at 20:40

1 Answers1

0

Seems there is no way to do this automatically. You need to clear the interval manually. You might define a class like this:

class TimerTask {
  constructor(time){
    this.id = setTimeout(() => this.run(), time);
  }

 run(){ /* override that in subclasses */ }

  clear(){
    clearTimeout(this.id);
  }
}

So one can do:

class Log extends TimerTask {
  run(){
    console.log("done");
  }
}

const test = new Log(1000);

Then somewhen:

test = test.clear();
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • 1
    `delete` is kinda misleading. `test = new Log; test.delete(); test.constructor(1000)`, and it doesn't lead to `test` being delete from anything. Why not just call it `clear` or `stop`? – Bergi Feb 20 '18 at 18:36