0

I am subscribing to an observable within a method and need to unsubscribe from it within another. The subCounter() method is called from an init function and the contents work just fine.

subCounter() {
  this.fml = this.playerService.counter(this.song).subscribe(data => {
    this.pos = data;
    this.time.position = Math.round(this.pos);
    this.time.dur = this.song.duration - this.time.position;
    this.time.durMinutes = Math.floor(this.time.dur / 60);
    this.time.durSeconds = ('0' + Math.ceil(this.time.dur - this.time.durMinutes * 60)).slice(-2);
    this.time.posMinutes = Math.floor(this.time.position / 60);
    this.time.posSeconds = ('0' + Math.ceil(this.time.position - this.time.posMinutes * 60)).slice(-2);
    this.time.percent = this.time.position / this.song.duration * 100;
  })
  // This works just fine
  console.log(this.fml);
}

When I call my touchActivate() function, it contains an unsubscribe function to that variable stored, however it throws an error as this.fml.unsubscribe is undefined. Console logging this.fml returns in an undefined object.

touchActivate() {
  console.log(this.fml);
  this.fml.unsubscribe();
}

At the top of my class I have the variable defined: public fml: any;

4
  • How do you call touchActivate method? Commented Jun 25, 2016 at 4:43
  • @yurzui with an event listener for the touchstart event. Commented Jun 25, 2016 at 4:45
  • Could you show us the code for touchstart event listener? Commented Jun 25, 2016 at 4:48
  • @yurzui $('.range-slider').on("touchstart", this.touchActivate); Commented Jun 25, 2016 at 4:50

1 Answer 1

1

Try this:

$('.range-slider').on("touchstart", this.touchActivate.bind(this));

Or use arrow function to retain this:

$('.range-slider').on("touchstart", () => this.touchActivate());
Sign up to request clarification or add additional context in comments.

1 Comment

The arrow function worked perfectly for me. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.