时序与异步函数循环 [英] Timing loops with asynchronous functions
本文介绍了时序与异步函数循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能得到一个循环的执行时间与异步函数的循环内?
Is it possible to get the execution time of a loop with an asynchronous function inside of the loop?
下面将工作同步功能,但不是异步的:
The following would work for synchronous functions, but not asynchronous:
var amount = 100;
var start = new Date().getTime();
for(var i=0; i < amount; i++){
// function
}
var end = new Date().getTime();
var time = (end - start) / 1000;
如果是这样,我怎么能转换成上述code,使时间
持有的总时间的循环(和内部功能)需要运行值?
If so, how can I convert the above code so that time
holds the value of the total time the loop (and the function inside) takes to run?
推荐答案
您可以用这样的事情做;
You can do it with something like this;
var amount = 100;
var completionAmount = 0;
var start = new Date().getTime();
var end, time;
var theFunction = function(){
// bla bla
}
var calbackOfTheFunction = function(){
//bla bla
completionAmount++;
if(completionAmount === 100){
end = new Date().getTime();
time = (end - start) / 1000;
}
};
for(var i=0; i < amount; i++){
theFunction();
}
这篇关于时序与异步函数循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文