stop ajax for loop exection

Last Reply 4 days ago By dharmendr

Posted 4 days ago

Hi friends,

i need to stop ajax for loop exection when i click cancel button.. i have attached code..  but loop continuously executing.. pls help

   var FileUpload = function (endoscopicid) {
          var fileData = new FormData();
            var fileSize = 0;
            var totalPercent = 0;
            var files = $("#fileinputType").get(0).files;
            date = $('#fm_sel_Year').val();
            for (var i = 0; i < files.length; i++) {
                fileSize+=files[i].size;
            }
            var completedFiles = 0;
            for (var i = 0; i < files.length; i++) {                    
                var data = new FormData();
                data.append(files[i].name, files[i]);
                var percent = 0;
                var xhr = new window.XMLHttpRequest();
                var ajaxImageUpload = $.ajax({
                    type: "POST",
                    async: true,
                    url: "../FurnaceImageUpload/UploadCustomerFiles",
                    xhr: function () {
                        data.append('Year', $('#fm_sel_Year').val().substring(0, 4));
                        data.append('EndoscopeDate', $('#fm_sel_Year').val());
                        data.append('Plant', $('#fm_sel_Plant').val());
                        //   data.append('reference', $('#InputRefrence').val());
                        data.append('endoscopicid', endoscopicid);
                        xhr.upload.addEventListener("progress", function (evt) {
                            if (evt.lengthComputable) {
                                var percentComplete = evt.loaded / fileSize;
                                totalPercent += percentComplete * 100;
                                percent = Math.floor(totalPercent);
                                    if (percent < 99) {
                                    $("#progresslabel").text(percent + "%");
                                    $('#div_Progress').css('width', percent + '%');
                                }
                            }
                        }, false);
                        return xhr;
                    },
                    contentType: false,
                    processData: false,
                    data: data,
                    success: function (data) {
                        if (data != '-101') {
                            completedFiles++; //Counting number of files completed
                            $('#filecount').html('uploaded ' + completedFiles + ' of ' + files.length) //Displaying how many files are completed and how many remaining files are there
                            if (percent >= 100 && completedFiles >= files.length) {
                                var plant = $('#fm_sel_Plant option:selected').text(),
                               addedDate = $('#fm_sel_Year').val().substring(0, 4);
                                $('#fileinputType,#fm_sel_Year,#fm_sel_Plant,#InputRefrence,#InputComments').val('');
                                $("#progresslabel").text("100%");
                                $('#div_Progress').css('width', '100%');
                                setTimeout(function () {
                                    $('#modal-Progress').modal('toggle');
                                    $('#progresslabel').text('');
                                    $('#modal-Progress').addClass('hide');
                                    swal({
                                        title: "EndoScopy Images Uploaded Successfully",
                                        text: "",
                                        type: "success",
                                        confirmButtonText: "OK"
                                    },
                                    function (isConfirm) {
                                        if (isConfirm) {
                                            location.reload();
                                        }
                                    });
                                }, 700);
                            }
                        } 
                    },
                    error: function () {
                        $('#fileinputType').val('');
                        $('#modal-Progress').modal('toggle');
                        $('#progresslabel').text('');
                        $('#modal-Progress').addClass('hide');
                        swal('', 'Images not uploaded Please try again later..', 'error');
                    }
                });
           }       
        };



   $(document).on('click', '#btnPauseImageUploading', function (e) {  
         fileuploadajax.abort();          
        fileuploadajax = null;       
        });


 

You are viewing reply posted by: dharmendr 4 days ago.
Posted 4 days ago