anukuls
anukuls

Reputation: 285

Callback function not being called - SweetAlert

I am calling a sweetalert on basis of the result of an if statement.

    for (int i = 0; i < count; i++) {
        if (numValue == 0) {
            swal({
                    title: 'Warning',
                    text: "Hey there..!!",
                    type: 'warning',
                    showCancelButton: true,
                    confirmButtonText: 'Yes',
                    cancelButtonText: 'No',
                    confirmButtonClass: 'confirm-class',
                    cancelButtonClass: 'cancel-class',
                    allowOutsideClick: false
                }, function(isConfirm) {
                    if (isConfirm) {
                        console.log("isConfirm in if: " + isConfirm);
                        $scope.$apply(function() {
                            $scope.saveAfterPSConfirm();
                        });
                    } else {
                        console.log("isConfirm in else: ");
                        return;
                    }
                }


            );
        }
    }

This calls a warning sweet alert if numValue is 0. For sample, numValue is 0.

While calling this snippet, sweet alert warning message "Hey there..!!" comes but immediately dissolves, without waiting for any button click for numValue popup. The issue is callback function of numvalue sweet alert is not getting called.

Upvotes: 0

Views: 4039

Answers (1)

Canet Robern
Canet Robern

Reputation: 1069

I have brought a fiddle which is based on your code.

And this fiddle used SweetAlert2.

Check this

In my project, I've used return swal() so I applied your code to it which works well.

Code is as follows:

swal({
    title: 'Warning',
    text: "Hey there..!!",
    type: 'warning',
    showCancelButton: true,
    confirmButtonText: 'Yes',
    cancelButtonText: 'No',
    confirmButtonClass: 'confirm-class',
    cancelButtonClass: 'cancel-class',
    allowOutsideClick: false
}).then(function(isConfirm) {
    if (isConfirm) {
        console.log("isConfirm in if: " + isConfirm);
        $scope.$apply(function() {
            //$scope.saveAfterPSConfirm();
        });
        count += 1;
        if(count < 3){           // input count.length instead of 3
            $scope.callAlert(count);
        }
    } else {
        console.log("isConfirm in else: ");
        return;
    }
});

I made the function recursive which means that this function will call itself until count.length.

I hope my solution is helpful for you. :)

Upvotes: 2

Related Questions