I have the following code here (with some lines removed to make it more clear). When a user clicks an edit icon the editRow() function is called and this opens a model window. After this the code tries to save data every second by calling factory.submitItem. Everything works okay except if there's a problem saving the data.
How can I make this so that if factory.submit item entityResource.update fails then the intervals are cancelled and the code stops doing a save every second.
     var factory = {
        gridSetup: function ($scope) {
            $scope.editRow = function (row, entityType) {
                // modal stuff happens here
                window.setTimeout(function () {
                    window.setInterval(function () {
                        factory.submitItem($scope, $scope.modal.data);
                    }, 1 * 60 * 1000);
                    factory.submitItem($scope, $scope.modal.data);
                }, 1 * 60 * 1000);
            }
        },
        submitItem: function ($scope, formData) {
            var idColumn = $scope.entityType.toLowerCase() + 'Id';
            var entityId = formData[idColumn];
            switch ($scope.modal.action) {
                case "edit":
                    var entityResource = $resource('/api/:et/:id', { et: $scope.entityType }, { update: { method: 'PUT' } });
                    entityResource.update({ id: entityId }, formData,
                        function (result) {
                            angular.copy(result, $scope.modal.data);
                        }, function (result) {
                            // what to put here ?
                        })
                    break;
            }
        },
 
     
     
    