Angularjs 如何处理警报消息中的多个超时

Angularjs 如何处理警报消息中的多个超时,angularjs,twitter-bootstrap,Angularjs,Twitter Bootstrap,我在显示警报消息时遇到处理多个超时的问题。在我的应用程序中,当创建任务、播放任务、暂停任务和删除任务时,我会收到几条警告消息。我使用$timeout将所有警报的超时设置为5000 这是创建任务的代码:: $scope.create = function () { console.log('Taskkkkkkkkkk Title create function is called : '); for (var i = 0; i < $scope.tasks.l

我在显示警报消息时遇到处理多个超时的问题。在我的应用程序中,当创建任务、播放任务、暂停任务和删除任务时,我会收到几条警告消息。我使用$timeout将所有警报的超时设置为5000

这是创建任务的代码::

$scope.create = function () {
        console.log('Taskkkkkkkkkk Title create function is called : ');
        for (var i = 0; i < $scope.tasks.length; i++) {
            if ($scope.tasks[i].title === this.title) {
                $scope.duplicateTitle = true;
                return;
            }
        }
        var task = new Tasks({
            'title': this.title,
            'description': this.description,
            'duration': 0,
            'lastStart': '',
            'archive': false,
            'active': true
        });

        console.log('Taskkkkkkkkkk Title : ' + task.title);
        if (task.title) {
            task.$save(function (response) {
                $scope.alerts.push({
                    type: 'success',
                    msg: 'Task have been Added!!' + '&nbsp&nbsp&nbsp<button type="button" class="close" data-dismiss="alert" ng-click="closeAlert()">&times;</button>'
                });

                $scope.closeAlert = function (index) {
                    $scope.alerts.splice(index, 1);
                };

                $timeout(function () {
                    $scope.alerts.splice($scope.alerts.indexOf(alert), 1);
                }, 5000);

                $scope.tasks.unshift(response);
                //$scope.tasks = Tasks.query();
                $scope.title = '';
                $scope.description = '';
                $state.go('listTasks');
            }, function (errorResponse) {
                $scope.error = errorResponse.data.message;
            });
        } else {
            var message = 'Title cannot be blank';
            $scope.error = message;
        }
    };
但当我在5秒内单击多个任务时,最后一条任务警报消息将消失。但不是第一个,这意味着它正在破坏堆栈顺序,即FIFO原则。我正在控制器中写入警报消息


我希望任务按照出现的顺序消失。任何建议都会对我更有帮助。

在超时中,我刚刚为拼接函数添加了索引值

$timeout(function () {
    $scope.alerts.splice(index, 1);
}, 5000);

现在工作正常了…

请给我们一些代码来重现您的问题,我们可以在您的代码中猜测许多可能的错误,但这不是猜测游戏。我添加了创建任务的代码。对于其他功能,我也使用相同的超时功能。