[英]syntax to add setTimeout to function with callback
我正在尝试执行以下“异步操作(效果很好)”,以便“在到达目标位置的x英里处”。 Google Maps进程对速度有限制,在“ Over_Query_limit”开始在地图上绘制之前,我可以绘制的路线要多于10条。
我知道服务条款(每天2500个),我没有违反这些条款。
它与一个从中心点(pt)到的所需目的地(终点)阵列成一个循环,请问实现此操作的语法是什么? 我在这个站点和其他站点上已经读了很多书,可以看到该函数应该用引号引起来,但是通过异步调用我看不到如何做。
你可以看到我的糟糕尝试(注释掉了)
var delay=100;
for (var i = 0; i < endPoints.length; i++) {
//setTimeout(function() {
howfar(pt,endPoints[i],i,function(i,status,endPoint) {
//process results
});
//},delay;
}
function howfar(from,to,i,callback) {
//get the endpoint from the directions service
callback.call({},i,status,endPoint);
}
一如既往的感谢您的关注和帮助
确切的语法如下所示:
var delay = 100; // in milliseconds, 100 is a tenth of a second
setTimeout(function() {
howfar(pt,endPoints[i],i, function(i,status,endPoint) {
//process results
});
}, delay);
不过,一个快速的Google会证明这一点。
如果我理解正确你的问题,你需要等到howfar
函数返回加上一个固定的延时,然后才处理下一个endPoint
数组中?
我通常会设置一个迭代器函数,该函数可自行调度,直到没有更多要处理的项目为止。 就像是:
var delay = 100;
var i = 0;
//define a helper function
var measureNext = function() {
howfar(pt, endPoints[i], i, function(i,status,endPoint) {
//process results
//if there are still unprocessed items in the array, schedule
//the next after {delay} milliseconds
if(i++ < endPoints.length) {
setTimeout(measureNext, delay);
}
});
};
//start with the first argument
measureNext();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.