A simple example of how to use angular's $interval service. This service is an nice evolution of the window.setInterval/clearInterval methods. Here's my blog on this: http://dankahle.blogspot.com/2014/08/angulars-interval-service.html
Last active
August 29, 2015 14:05
-
-
Save dankahle/712f6a4fe234d96bb0c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html ng-app="app"> | |
<head> | |
<title>index.html</title> | |
<style> | |
</style> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.17/angular.min.js"></script> | |
</head> | |
<body ng-controller="ctrl"> | |
<script> | |
var app = angular.module('app', []); | |
app.controller('ctrl', function ($scope, $interval) { | |
$scope.$watch(function () { | |
console.log('watch'); // for watching the digest loop | |
}) | |
var p = $interval(function (val) { | |
console.log('interval fcn', val); | |
}, 100, 3, false); | |
p.then(function (val) { | |
console.log('success', val); | |
$scope.$digest(); | |
}, function (val) { | |
console.log('error', val); | |
$scope.$digest(); | |
}, function (val) { | |
console.log('notify', val) | |
}) | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output >>>>>
(2) watch // 2 digests before the loop
interval fcn 0
notify 0
interval fcn 1
notify 1
interval fcn 2
notify 2
success 3
watch // the $scope.$digest call from success fcn