在几个ajax请求期间,依赖函数在特定时间段被调用。我的问题是,有没有办法在不使用设置超时函数中的事件的情况下找到总时间段(200+300+500)。
//main ajax calls using following functions
setTimeout(function(){
avg(10,15,30);
alert('I am triggered at 2ms');//dependant function 1 (calculate avg)
},200);
setTimeout(function(){
total(50,100,30);
alert('I am triggered at 3ms');//df 2(calculate total)
},300);
setTimeout(function(){
vat_cal(180,12.5);
alert('I am triggered at 5ms');//df 3(calculate vat % for total)
},500);
假设我不知道使用了多少次设置超时。因此,如果时间因素已知,则可以更轻松地加载带有通知的数据。
多个 ajax 请求正在消耗数据加载时间。如果我知道总时间(200+300+500 = 1000)。我可以通知用户等待最多一秒钟。
function avg(x1,x2,x3)
{
alert(x1+x2+x3)
}
function total(x1,x2,x3)
{
alert(x1+x2+x3)
}
function vat_cal(x1,x2,x3)
{
alert(x1+x2+x3)
}
$.when( avg(10,15,30),total(50,100,30), vat_cal(180,12.5) ).done(function( x,y,z ) {
alert("finish")
});
你的问题似乎有点模糊。但是如果你想重复setTimeout()
函数几次,最好使用setInterval()
函数。我真的没有得到你想要的结果,但它应该有效。
[Code is Here][1]
[1]: http://jsfiddle.net/asomani/2y0t60g5/2/
Use When and Then callback method and in the last ajax request complete call the time Calculate Method.
window.time1 = new Date().getTime();
function aCallback()
{
window.time2 = new Date().getTime();
total_time = (window.time2 - window.time1)
}
$.when($.ajax({
data: {test:"1"}
}), $.ajax({
data: {test:"1"}
}),$.ajax({
data: {test:"1"},
success: aCallback
}) ).done(function( x,y,z ) {
alert(total_time );
});