Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在for循环中的while循环中调用ajaxapi?_Javascript_Jquery_Ajax_Api - Fatal编程技术网

Javascript 在for循环中的while循环中调用ajaxapi?

Javascript 在for循环中的while循环中调用ajaxapi?,javascript,jquery,ajax,api,Javascript,Jquery,Ajax,Api,因此,我正在为一个类创建一个API mashup。现在,我从GoogleMapsAPI得到一个json响应。通过使用for循环,我从响应中获得了5个航路点。我试图解决的问题是,一些coords处于死角,没有与它们相关的图片。我想在4次迭代中的每一次都调用ajax,但是如果一次没有图片,那么它会迭代直到找到一个,然后返回到它停止的地方。 我尝试使用一个while循环,这样它就可以进行调用,在一个成功的坐标中,while循环变量将被设置为true,然后它将中断while循环,然后返回for循环,该循

因此,我正在为一个类创建一个API mashup。现在,我从GoogleMapsAPI得到一个json响应。通过使用for循环,我从响应中获得了5个航路点。我试图解决的问题是,一些coords处于死角,没有与它们相关的图片。我想在4次迭代中的每一次都调用ajax,但是如果一次没有图片,那么它会迭代直到找到一个,然后返回到它停止的地方。 我尝试使用一个while循环,这样它就可以进行调用,在一个成功的坐标中,while循环变量将被设置为true,然后它将中断while循环,然后返回for循环,该循环将迭代到下一个“航路点”。到目前为止,我没有收到错误,该网站正在超时,可能太多的电话?也许我在做一些递归的事情

// so this for loop divides the hundreds of steps by 1/4 of the total length.
for (i=0; i<polyPoints.length; i+=quarters) {
    // gets the coords for the current iteration.
    var lat = polyPoints[i][0];
    var lng = polyPoints[i][1];

    var hasPic = false;
    var origI = i;

    //while loop runs through and checks to see if the coordinate has pictures at it.
    while (hasPic == false){
        $.ajax({
            type : "GET",
            dataType : "jsonp",
            ajaxI: i,
            url: 'https://api.instagram.com/v1/media/search?lat='+lat+'&lng='+lng+'&distance=5000&access_token='+token,
            success: function(data){
                i = this.ajaxI;
                //if null,increases the iteration by one, and then runs through the loop again, checking the next coordinate? I hope.
                if(typeof data.data[0] === "undefined" || data.meta.code == 400){
                    i++;
                    console.log("i increased to "+i);
                }else{
                    //if the pic is there then it assigns a random picture from the result to the images array.
                    images.push(data.data[Math.floor(Math.random() * data.data.length)].images.low_resolution.url);

                    //sets while loop to stop
                    hasPic = true;

                    //loads the current iterations coordinates for the later for loop to create markers.
                    waypoints.push([lat,lng]);
                }
            }, //data.data[0].images.low_resolution.url
            error: function(data){
                i = this.ajaxI;
                i++;
                //images.push(img/test.jpg");
            }
        }).responseText;
    }//waypoints.push([lat,lng]);

    //resets the i back to the original iteration before it got increased
    i = origI;
}
//所以这个for循环将数百步除以总长度的1/4。
对于第14行上的(i=0;i三重相等(==)?

我想你的意思是==?

这是我的尝试,使用
$。延迟

function tryGetImage(firstI, maxI) {
    var dfr = $.Deferred();
    var inner = function(i) {
        if(i >= maxI) dfr.reject();

        var lat = polyPoints[i][0];
        var lng = polyPoints[i][1];
        $.ajax({
            type : "GET",
            dataType : "jsonp",
            url: 'https://api.instagram.com/v1/media/search?lat='+lat+'&lng='+lng+'&distance=5000&access_token='+token,
        }).done(function(data){
            //if null,increases the iteration by one, and then runs through the loop again, checking the next coordinate? I hope.
            if(typeof data.data[0] === "undefined" || data.meta.code == 400){
                inner(i + 1);
            }else{
                var img = data.data[Math.floor(Math.random() * data.data.length)].images.low_resolution.url);
                dfr.resolve(img, [lat, lng]);
            }
        ).fail(function(data){
            inner(i + 1);
        });
    };
    inner(firstI);
    return dfr.promise();
}

var dfrs = [];
for (i=0; i<polyPoints.length; i+=quarters) {
    var dfr = tryGetImages(i, i + quarters)
    dfr.done(function(img, coords) {
        images.push(img);
        waypoints.push(coords);
    })
    dfrs.push(dfr);
}
$.when.apply($, dfrs).always(function() {
    console.log("All requests complete");
});
函数tryGetImage(firstI,maxI){
var dfr=$.Deferred();
var内部=函数(i){
如果(i>=maxI)dfr.reject();
var lat=多点[i][0];
var lng=多点[i][1];
$.ajax({
键入:“获取”,
数据类型:“jsonp”,
网址:'https://api.instagram.com/v1/media/search?lat=“+lat+”&lng=“+lng+”&distance=5000&access_token=”+token,
}).完成(功能(数据){
//如果为null,则将迭代次数增加1,然后再次运行循环,检查下一个坐标?我希望如此。
if(数据类型.data[0]==“未定义”| | data.meta.code==400){
内(i+1);
}否则{
var img=data.data[Math.floor(Math.random()*data.data.length)].images.low_resolution.url);
dfr.resolve(img,[lat,lng]);
}
).失败(功能(数据){
内(i+1);
});
};
内(第一);
返回dfr.promise();
}
var-dfrs=[];

对于(i=0;i您的代码当前位于一个无限循环中,发出ajax请求。可能我错过了一些东西,但是您在while循环中的lat&lng变量在哪里更新?while循环似乎会一次又一次地检查相同的协调,如果没有找到pic。这会在无限循环中失败,不是吗?不,
==
是有效的(而且通常更喜欢)。