javascript - Handling no longer needed async data in AngularJS -
here situation: have user spams input on angularjs app, , of course creating kinds of async data calls on backend. problem is, each of these data fetchs come back, user see data change multiple times on screen , data user rested on 1 comes in last that's see.
but can't count on because async means order isn't guaranteed. there possibility data page c arrives before data page b user skipped on without waiting display. now, user sees data page b because returned last think seeing page c. how protect against this?
$http accepts canceller parameter, promise can used cancel previous request. want cancel request when you're make new request. here rudimentary example. note best practices not suggest using $http within controller.
var canceller = $q.defer(); $http.get("/api/movies/slow/2", { timeout: canceller.promise }) .then(function(response){ $scope.movie = response.data; }); $scope.cancel = function(){ canceller.resolve("user cancelled"); }; read more here: http://odetocode.com/blogs/scott/archive/2014/04/24/canceling-http-requests-in-angularjs.aspx
Comments
Post a Comment