I'm using angularjs in my project. I come across a problem. There are two buttons A and B on the page, they call same function which gets remote data from server. When i click A then click B, it sends two requests, but sometimes request B returned earlier then request A, so last i render data from request A on the page which I should render data from request B. Now I have a solution below:
app.controller("controller", function($scope) {
$scope.lastTimeStamp = null;
$scope.getData = functio() {
$scope.lastTimeStamp = new Date().getTime();
var temp = angular.copy(t);
$http({}).success(function() {
if($scope.lastTimeStamp == temp) {
// this is the data from last request
}
})
}
});
every time call $scope.getData(), I update $scope.lastTimeStamp and var temp with same value to ensure $scope.lastTimeStamp is the actual last one, and compare it with temp in the call back. This works. But this situation is common is my project, if solve it in this way, every time I need to do this which has non business logic with business logic. so I wonder if there is a common solution to solve this? any help?