I'm using Angular's timeout to refresh the contents of an image at a user-specified interval. When that interval is too low for the image to load (e.g. 1 second), each subsequent request cancels the previous one causing the image to never refresh.
Instead of this behavior, I would like for the current request to complete and for subsequent requests to be canceled until that one is completed when requests collide. How can I do this?
The code that handles the refreshing:
$scope.setImagePath = function() {
$scope.imagePath = $scope.camera.endpoint + '?decache=' + Math.random();
};
$scope.setImagePath();
$scope.refreshOnInterval = function() {
$timeout(function() {
$scope.setImagePath();
$scope.refreshOnInterval();
}, $scope.refresh);
};
$scope.refreshOnInterval();