0

我正在尝试使以下“我将在 x 英里内向这些目标地点行驶的地方”异步功能(效果很好)也在执行之间等待。谷歌地图过程对速度有限制,在“Over_Query_limit”开始绘制地图之前,我可以绘制的路线多于 10 条。

我知道服务条款(2500/天),我不会违反它们。

它位于一个循环中,其中包含来自中心点(pt)的所需目的地(endPoints)数组,请问实现这一点的语法是什么?我在这个网站和其他网站上阅读了很多,并且可以看到该函数应该放在引号中,但是通过异步调用我看不出如何。

你可以看到我糟糕的尝试(注释掉)

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

一如既往地感谢您的关注和帮助

4

2 回答 2

1

If I understood your question correctly, you need to wait until the howfar function returns plus a fixed delay, and only then process the next endPoint in the array?

I typically setup an iterator function which schedules itself until there are no more items to be processed. Something like:

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();
于 2013-01-22T22:45:50.793 回答
1

确切的语法如下所示:

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

不过,一个快速的谷歌会发现这一点。

于 2013-01-22T22:36:43.897 回答