0

How can I remove a TimerInterval set by the famo.us Timer Class (I do not completely understand how famo.us wraps the natvie JS-setInterval method). In native JS I can do:

var id = setInterval(fn, delay);
clearInterval(id);

I do not want to use the above solution, since it is not recommended by famo.us. Ho can I achieve the above with the famo.us Timer Class?

Timer.setInterval( function(){
    console.log('exec function');
    //this.doSomething() is a function defined on the classes prototype
    this.doSomething();
    }.bind(this), 3000);

//how can I referenc the above function?!
Timer.clear(/*function*/);

I need the setInterval Function to be bound to 'this', since I use the 'this'-context in the interval execution

Any help is much appreciated.

talves
  • 13,993
  • 5
  • 40
  • 63
doemsche
  • 892
  • 2
  • 12
  • 25

2 Answers2

1

Add a reference to the function you are using, because setInterval returns the function reference.

Working Example code here in the Splash view: and below:

this.count = 0;
this.timerFunc = Timer.setInterval( function(){
  this.count += 1;
  this.surface.setContent('count ' + this.count);
}.bind(this), 1000);

this.surface.on('click', function(){
  Timer.clear(this.timerFunc);
}.bind(this));
talves
  • 13,993
  • 5
  • 40
  • 63
0

Something like this would probably work:

var fn = function(){
    console.log('exec function');
}
Timer.setInterval(fn, 3000);
// Time passes
Timer.clear(fn);
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
  • Thanks bali182. That's true. But the problem is, that I have bound the setInterval to 'this', since I need this-context within the intervall-call. If I don't bind it to 'this' it this is the EventHandler, which is no use to me... – doemsche Oct 16 '14 at 15:02
  • bali182 is correct, although you wanted your use case, so I included it in my answer. In his example you could have just used `this.fn` – talves Oct 17 '14 at 21:47