How to pause a FOR loop in Javascript in a function?

google-places-api, javascript

Solution

Recursive solution,

function requestInfoWrapper(results, i) {
    i = i + 1;
    if (i >= results.length) {return};
    requestInfo(results[i]);
    setTimeout(function() {requestInfoWrapper(results, i);}, 1000);
}

Some example code to test it,

   var results = ["test 1", "test 2", "test 3"];
   function requestInfo(str) {console.log(str);}
   requestInfoWrapper(results, -1);

Also integrated with your code would just be,

function callback(results, status) {
    if (status != google.maps.places.PlacesServiceStatus.OK) {
        alert(status);
        return;
    }

    requestInfoWrapper(results, -1);
}

Problem

There's a query per second limit in Google Places API, so I need to slow down my loop which sends requests: ``` function callback(results, status) { if (status != google.maps.places.PlacesServiceStatus.OK) { alert(status); return; } for (var i = 0, result; result = results[i]; i++) { RequestInfo(result); } } ``` Any idea how I can do that? I'm a bit of a newb.

Original source

Related problems