在ajax内调用另一个ajax,直到第一个完成 [英] Call another ajax inside ajax till first will complete
问题描述
我正在使用jquery ajax在我的Web中执行某些操作.我想每隔一秒钟调用另一个ajax,直到第一个ajax响应出现.我的ajax代码是:-
I am using jquery ajax for performing some operation in my web. I want to call another ajax in every seconds until the first ajax response come. My ajax code is :-
$.ajax({
url : $(this).attr('action'),
type : $(this).attr('method'),
dataType: 'json',
data : $(this).serialize(),
beforeSend: function() {
var row = '<tr id="row_'+i+'">';
row += '<td>'+$('#fileid').val()+'</td>';
row += '<td>'+$('#resolution').val()+'</td>';
row += '<td>'+$('#bitrate').val()+'</td>';
row += '<td>'+$('#ndrive').val()+'</td>';
row += '<td>'+$('.type').val()+'</td>';
row += '<td>'+curr_date+'-'+curr_month+'-'+curr_year+'</td>';
row += '<td>'+hours+':'+minute+':'+second+'</td>';
row += '<td><div id="process-bar" class="process-bar" style="height: 18px;"> </div></td>';
row += '</tr>';
$('#stastistics tbody').prepend(row);
$('#fileid').val('');
$('#ndrive').val('');
$('.type').val('');
$('#resolution').val('');
$('#bitrate').val('');
},
success : function(data)
{
if(data.status == 'error')
{
var html = '<span class="label label-danger">Error</span>';
$("tr#row_"+i+" td:nth-child(8)").find('#process-bar').remove();
$("tr#row_"+i+" td:nth-child(8)").html(html);
}
else if(data.status == 'success')
{
var html = '<span class="label label-success">Success</span>';
$("tr#row_"+i+" td:nth-child(8)").find('#process-bar').remove();
$("tr#row_"+i+" td:nth-child(8)").html(html);
}
},
error : function( xhr, err )
{
},
});
这是ajax需要很长时间才能完成,所以我想显示此过程的进度.所以我想每秒钟调用另一个ajax,以便每秒钟更新进度数据.在第一个ajax响应之前,如何在此ajax中调用另一个ajax. 感谢您的帮助.
This is ajax take a long time to complete so i want to show a progress for this process. so i want to call a another ajax in every second so it update the progress data in every second. How can i call another ajax inside this ajax till response of first ajax. Thanks for help.
推荐答案
我会做这样的事情:
var progressUpdater = setInterval(function()
{
$.ajax({
url: addressToProgressEndpoint,
method: METHOD
})
.done(function(data)
{
//update UI with progress
});
}, 1000);
在成功回调中的原始ajax请求中,在progressUpdater上调用clearInterval()
将其停止
In your original ajax request in the success callback, call clearInterval()
on progressUpdater to stop it
这篇关于在ajax内调用另一个ajax,直到第一个完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!