1

Behold, my code!

I'd like to increase the elapsedTime by 100 every tick, but it seems to only fire once. I can't quite figure this one out. It fires once, but won't repeat.

var gravity = angular.module('gravity', []);

    function GravCtrl($interval) {
      this.acceleration = 9.8;
      this.elapsedTime = 0;
      this.speed = 0;
      this.$interval = $interval;
    }

    GravCtrl.prototype.start = function() {
      this.counter = this.$interval(function(that) { 
        that.elapsedTime += 100;
        that.speed = ( that.elapsedTime / 1000.0 ) * that.acceleration;
        console.log('thing firing');
      }(this), 100)
    };

    gravity.controller('GravCtrl', GravCtrl);
opticon
  • 3,494
  • 5
  • 37
  • 61
  • 1
    You invoke the function and pass `undefined` to `$interval`. – a better oliver Jun 25 '15 at 15:42
  • Remove `(this)` from there and `var that = this` just above the interval call. You are invoking it right away instead of providing the function reference to the interval. – PSL Jun 25 '15 at 15:44
  • A quick look at the console should reveal some information. – timtos Jun 25 '15 at 15:45
  • I need to be able to access the variables in my GravCtrl object from within the interval. Without passing them into the function somehow, they're unavailable. How do I go about access the variables within the interval? – opticon Jun 25 '15 at 15:47
  • @opticon See my comment above, `var that = this` and remove `that` from `function(that)` .If you want to use `this` then do `.bind(this)` instead of `(this)`. You should read http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback. – PSL Jun 25 '15 at 15:49

0 Answers0