每10秒调用一个ajax函数,如果成功,停止,否则继续,直到成功

每10秒调用一个ajax函数,如果成功,停止,否则继续,直到成功,ajax,Ajax,我正试图通过ajax调用下载一个文件。 它应该每20秒处理一次。 如果文件存在,它将被下载,并应停止进一步的ajax调用,否则应处理相同的ajax,直到文件被下载。 下面是我尝试过的 $.ajax({ url: baseUrl + '/ajax/exportStudentInformation', data: { 'studentId' : studentId

我正试图通过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);
                }
            },
        }); 
    },
$.ajax({
url:baseUrl+'/ajax/exportStudentInformation',
数据:{
“studentId”:studentId
},
类型:“post”,
成功:功能(响应){
setTimeout(函数(){
timeIntervalDownLoadFile=下载文件(学生姓名);
}, 60000);
},
错误:函数(){
警报(“处理csv文件时出错,请重试”);
}
});
这是需要检查文件是否存在于云中并下载的函数。
下载文件:函数(studentname){
$.ajax({
url:baseUrl+'/ajax/downloadFile',
数据:{
“studentname”:studentname
},
'type':'POST',
错误:函数(jqXHR、状态、错误){
警报('下载文件时出错,请重试!!!');
},
中止:函数(jqXHR、状态、错误){
},
beforeSend:function(){
var message=“正在下载文件,请稍候”;
$('#alertBar').html(“+message+”)
$(“#alertBar”).fadeTo(2000500).slideUp(500,function(){
$(“#alertBar”)。slideUp(500);
});
$(“html,body”).animate({scrollTop:$('#alertBar').offset().top},1000);
}
},
}); 
},

我使用jquery的set interval在延迟后调用第二个函数time。但一旦显示文件下载弹出窗口,它不会停止。任何帮助都将被通知。

您可以使用
let timeout=0
然后
timeout=setTimeout(…)
。然后在您的函数中,
clearTimeout(timeout)
成功。是的,我尝试过这种方法,错过了添加文件的那一部分,显示了下载弹出窗口,但ajax调用仍然得到处理,不是吗stops@ExplosionPills不确定,它不会停止。。。