2014-12-03 31 views
0

在几个ajax请求期间,在某个时间段调用依赖函数。我的问题是,有什么方法可以在没有使用settimeout函数中的事件的情况下查找总时间段(200 + 300 + 500)。多个settimeout调用的时间段计数

//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); 

假设我不知道setTimeout使用了多少次。 因此,如果时间因素已知,则可以更轻松地通过通知加载数据。

多个ajax请求正在查杀数据加载时间。如果我知道总时间(200 + 300 + 500 = 1000)。我可以通知用户等待一秒钟。

+1

我不知道你想描述一下。你能举一个更具体的例子说明你正在做什么? – JLRishe 2014-12-03 06:38:57

+0

请您详细说明您的问题.. – 2014-12-03 06:42:55

回答

0
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") 
}); 
+0

感谢您的信息.... – narasimharaosp 2014-12-03 09:13:19

0

你的问题似乎有点含糊。但是如果您想多次重复setTimeout()函数,最好使用setInterval()函数。 我真的没有得到你想要的结果,但它应该工作。

+0

感谢它可能是模糊的,但需要输出清晰...没有违法,如果我的坏... – narasimharaosp 2014-12-03 09:11:32

0
[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); 

});