Javascript 使用angular操作异步调用
我有一个显示MongoDB事件的日历。 每次用户单击prev或next按钮时,我都会执行一个异步调用,然后重置日期和事件的当前数组;但是,如果用户多次单击某个按钮,将显示双重结果Javascript 使用angular操作异步调用,javascript,angularjs,mongodb,Javascript,Angularjs,Mongodb,我有一个显示MongoDB事件的日历。 每次用户单击prev或next按钮时,我都会执行一个异步调用,然后重置日期和事件的当前数组;但是,如果用户多次单击某个按钮,将显示双重结果 $scope.next = function () { //reset array $scope.gridOptions.data=[]; //date of week myService.dbAction(dt3,dt4).then(function (data) {
$scope.next = function () {
//reset array
$scope.gridOptions.data=[];
//date of week
myService.dbAction(dt3,dt4).then(function (data) {
check(data);
$scope.gridOptions.data = $scope.setts;//update data
}, function (error) {
console.error('ERROR: ' + error);
})
}
我只想要上周的事件,怎么做?一个快速解决方法是添加一个
fetchPending
标志。使用它可以避免重复获取
$scope.next = function () {
if ($scope.fetchPending) return;
$scope.fetchPending = true;
//reset array
$scope.gridOptions.data=[];
//date of week
myService.dbAction(dt3,dt4).then(function (data) {
check(data);
$scope.gridOptions.data = $scope.setts;//update data
}).catch ( function (error) {
console.error('ERROR: ' + error);
}).finally( function () {
$scope.fetchPending = false;
})
}
视情况而定。是否要阻止用户单击上一步/下一步,直到返回结果?或者你想让他们尽可能快地点击按钮,但只保留最新信息吗?我想显示最新信息