0

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;
            }
        },
4

3 回答 3

2
myInterval = window.setInterval(function () {
    .....
}, 1 * 60 * 1000);

当你想取消时......

window.clearInterval(myInterval);
于 2013-10-24T08:04:40.513 回答
1

setInterval()返回一个间隔 ID,您可以将其传递给 clearInterval():

var yourInterval = setInterval(function(), time);

您可以通过以下方式取消它:

clearInterval(yourInterval);
于 2013-10-24T08:09:49.110 回答
0

你可以做一些不同的事情来做到这一点。该方案可能是这样的:

var everythingIsOk = true;

function doSomething(){
    if (everythingIsOk) {
        setTimeout(doSomething(),5000);
    } else {
       everythingIsOk = false;
       return true;
    }
}
于 2013-10-24T08:14:07.333 回答