Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
100
rated 0 times [  105] [ 5]  / answers: 1 / hits: 34184  / 13 Years ago, wed, february 1, 2012, 12:00:00

I would like a add a 1-2 second delay on each iteration of the following loop.



<html>
<script type=text/javascript src=http://maps.googleapis.com/maps/api/js?sensor=false></script>
<script type=text/javascript src=http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js></script>

<input id=start type=submit> </input>
<div id='status'></div>

<script>
var geocoder=new google.maps.Geocoder();
var glGeocodeCount = 0 ;

$(document).ready(function() {

$('#start').click(function() {

//srPerformGeocode(TD Tower, 55 King Street West, Toronto, ON, Canada, M5K 1A2);

for(x=0;x<20;x++){
srPerformGeocode(TD Tower, 55 King Street West, Toronto, ON, Canada, M5K 1A2);
}
return false;
});
});

function srPerformGeocode(address){
if (geocoder){
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK){
$('#status').prepend(Success : + address + <br/>);

}
else{
$('#status').prepend(Failed : + address + <br/>);

}
});
}
}
</script>

More From » loops

 Answers
28

You can do it this way with setTimeout():



$(document).ready(function() {
$('#start').click(function() {
//srPerformGeocode(TD Tower, 55 King Street West, Toronto, ON, Canada, M5K 1A2);
var x = 0;

function go() {
srPerformGeocode(TD Tower, 55 King Street West, Toronto, ON, Canada, M5K 1A2);
if (x++ < 20) {
setTimeout(go, 2000);
}
}
go();

return false;
});
});


This does make me wonder why you're doing a geocode lookup on the exact same address 20 times in a row?


[#87703] Monday, January 30, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kerryoliviaa

Total Points: 221
Total Questions: 102
Total Answers: 117

Location: Sint Maarten
Member since Tue, Mar 29, 2022
2 Years ago
;