| angular.module('yourModule').service('pollSvc', function($interval) { | |
| var svc = this; | |
| this.start = startPolling; | |
| this.stop = stopPolling; | |
| function startPolling() { | |
| if (svc._int) throw new Error("Already polling!"); | |
| svc._int = $interval(poll, 3000); | |
| } | |
| function stopPolling() { | |
| if ( angular.isNull(svc._int) ) return; | |
| $interval.cancel(svc._int); | |
| svc._int = null; | |
| } | |
| function poll () { | |
| $http.get('state.json'). | |
| success(function (data, status, headers, config) { | |
| svc.markers = data; | |
| // may want to keep a buffer of last X poll calls in an array instead of overwriting it every time. | |
| }). | |
| error(function (data, status, headers, config) { | |
| // log error | |
| }); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment