Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$interval not running, angularjs

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);
});
like image 682
Steven Harlow Avatar asked Sep 29 '26 10:09

Steven Harlow


1 Answers

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);
like image 134
Mohamed Youssef Avatar answered Oct 02 '26 01:10

Mohamed Youssef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!