javascript - What's the best way to control the speed of a loop? -
I am using Google's GeoCoder API and I'm killing rate limits using the Loop below. What is the best way to slow down? You can specify the result of a Result The length of the array varies, but does not exceed 50 items data for
{var results = data [key]; Var address = result ['address']; // test if (geocoder) {geocoder.geocode ({'address': address}, function (result, condition) {if (status == google.maps.GeocoderStatus.OK) {if (status! = Google.maps GeocodrastStats .ZERO_RESULTS) {map.setCenters (Results [0] .geometry.location); var infowindow = new google.maps.InfoWindow ({content: '& lt; b & gt;' + + + + + + '& lt; / b & Gt; ', size: new google.maps.size (150,50)} var marker = new google.maps.marker ({status: result [0] .geometry.location, map: map, title: address} ); Google.maps Event.addListener (marker, 'click', function () {infowindow.open (map, marker);});} and {warning ("no results found");}} else {alert ( "Geocode was not successful for Mnlikit reasons: "+ position);}}); }
Put all the code inside your loop inside a function called < / Code> Then use this code to call that function at a specified interval:
var numberOfMilliseconds = 1000 setInterval (mySuperCoolLoopFunction, numberOfMilliseconds) < P> You can read about all Javascript set interval function as explained by Possy. The first argument is a function, and the second argument is the number of milliseconds to wait before calling the function again.
setInterval call in a variable and it will allow you to stop the interval at some point. Like this:
myInterval = setInterval (coolFunction, 1000) ... clear interval (myInterval)
Comments
Post a Comment