3

I'm not sure why this code isn't running. I'm trying to set up a simple counter that increases every second using angulars $interval wrapper.

angular.module('app').controller('testController', function($scope, $interval){
  var set_counter = function(){
    var start_time = new Date(2014, 09, 02, 0,0,0,0).getTime()
    var time_counter = new Date().getTime()
    $scope.counter = Math.ceil(time_counter / 1000000 + ((time_counter / 1000 - start_time / 1000) * 0.5)).toLocaleString();
    console.log($scope.counter);
  }
  $interval(set_counter(), 1000);
});
1
  • 4
    You're calling set_counter immediately (set_counter()), and passing the result (undefined) to $interval. $interval expects a reference to a function, you need to just pass a reference to set_counter to $interval - $interval(set_counter, 1000); Commented Oct 9, 2014 at 18:32

1 Answer 1

1

The issue is in on that line

$interval(set_counter(), 1000);

you are calling the "set_counter" right away not passing it to "$interval" as a callback.

Fix:

$interval(set_counter, 1000);
Sign up to request clarification or add additional context in comments.

4 Comments

And how is that different from the comment provided that worked out the problem?
You could have upvoted the comment instead of answering here. As @AayushiJain mentioned, its not different from what has been commented.
you are right I just didn't notice the comment that you are talking about I thought no one answered at all that's it and It won't harm answering a question with different words Thanks anyway
The comment should have been an answer. Duh.

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.