每10秒后调用一次ajax函数,如果成功则停止,直到成功为止

时间:2018-04-19 15:07:23

标签: ajax

我想通过ajax调用下载文件。 它应该每20秒处理一次。 如果文件存在,它将被下载并将停止进一步的ajax调用,否则应该处理相同的ajax直到文件下载。 以下是我试过的内容

 $.ajax({
                url: baseUrl + '/ajax/exportStudentInformation',
                data: {
                    'studentId' : studentId
                },
                type: "post",

                success: function(response) {
                    setTimeout(function () {
                        timeIntervalDownLoadFile = downloadFile(studentname); 
                    }, 60000);
                },
                error: function(){
                    alert('There was error in processing csv file, please try again');
                }
            });

This is the function that needs to check if the file is present on the cloud and download it.

downloadFile: function(studentname) {

        $.ajax({
            url: baseUrl + '/ajax/downloadFile',
            data: {
                'studentname': studentname
            },
            'type': 'POST',
            error: function(jqXHR, status, error) {
                alert('There was error in downloading file, please try again!!!');
            },
            abort: function(jqXHR, status, error) {
            },
            beforeSend: function() {
                var message = "The file is being downloaded, please wait";
                $('#alertBar').html("<div class=\"alert alert-info\" >"+message+"<a class=\"close\">&times;</a></div>")
                $("#alertBar").fadeTo(2000, 500).slideUp(500, function(){
                   $("#alertBar").slideUp(500);
                });
            },
            success: function(response) {
                if (response.status) {
                    window.location.href = "https://urlwhereFileisStoredOnCloud/"+response.filename;
                } else {
                    var message = "File does not exist, please use export to generate file";
                    $('#alertBar').html("<div class=\"alert alert-danger\" >"+message+"<a class=\"close\">&times;</a></div>")
                    $("#alertBar").fadeTo(2000, 500).slideUp(500, function(){
                       $("#alertBar").slideUp(500);
                    });
                    $("html, body").animate({ scrollTop: $('#alertBar').offset().top }, 1000);
                }
            },
        }); 
    },

我已经使用jquery的set interval来在延迟后调用第二个函数时间。但是一旦显示文件下载弹出窗口,它就不会停止。任何帮助都将得到解决。

0 个答案:

没有答案