Situation
Currently, I am interested in developing a web application that tracks a specific data set based on the time since the page was loaded. For example, "how many calories have you burned since opening this webpage?"
I am still trying to grasp the concept of AngularJS services, factories, etc., and I am curious about the most efficient way to create an automatically updating Timer that can be used to constantly (every second) manipulate and update ng-model.
My Attempt with Limited Success:
Here is what I have tried so far:
app.factory('Timer', function($timeout) {
var time = 0;
var Timer = function() {
this.time++;
this.timeout = $timeout(this.Timer, 1000);
}
});
This is how I attempted to use it:
$timeout(function() {
$scope.someNgModelVarForTheView = Timer.time * $scope.data;
}, 1000);
Unfortunately, my imagined solution does not work as intended. I seem to be missing the correct approach to accomplish this task...
Therefore, I have two main questions:
- How can I calculate the time elapsed since the page was loaded, as a callable function?
- What is the best method for recalculating the data model consistently (every second)? Is $timeout the right tool for this job?