I have the following code:
for (var i = 0; i < markers.length; i++) {
var lat = markers[i][0];
var lng = markers[i][1];
var img = markers[i][2];
var info = markers[i][3];
setTimeout(function(lat, lng, img, info) {
console.log(lat + ', ' + lng);
$('#map').gmap3({
action: 'addMarker',
latLng:[lat, lng],
options:{
animation: google.maps.Animation.DROP,
icon: img
},
events:{
click: function(marker, event, data){
$(this).gmap3({action:'addinfowindow', anchor:marker, options:{content: '<div id="content" style="width:300px;height:250px;"><img src="' + info + '"></img></div>'}});
/*var infowindow = $(this).gmap3({action:'get', name:'infowindow'});
infowindow.close();*/
},
}
});
}, i* 100);
}
The console.log is showing undefined for lat and lng. Why is this?
Previously I didn't pass any variables into the function within timeout and they WERE defined but it used the same one for each marker in the for loop which was obviously wrong!
Any ideas?
setTimeout accepts a reference to a function as the first argument. This can be the name of a function: A variable that refers to a function (a function expression): Or an anonymous function:
By definining a function as the first parameter to setTimeout it is possible to pass a variable. As pointed out by a couple of commenters below, setTimeout has a variable parameter list so you can simply keep adding the extra parameters to the end of the function call like so, where 5000 is the timeout and "msg1" and "msg2" are the parameters:
setTimeout is best used for everything else. Note: if you need to repeatedly execute code after a specified delay, then setInterval is more suited to the job. You can read more about this function here.
A common issue when using the setTimeout function is passing in the function to execute correctly. Often, the function is passed as a function call instead of the function signature. Solution: remove the parentheses ()
lat
and lng
are named parameters of your timeout function, but setTimeout
does not pass any parameters to that function, so they remain undefined.
You should move the timeout setup into a separate function, in order to establish a closure for your variables:
function configureTimeout(i, lat, lng, img, info) {
setTimeout(function() {
console.log(lat + ', ' + lng);
$('#map').gmap3({
action: 'addMarker',
latLng:[lat, lng],
options:{
animation: google.maps.Animation.DROP,
icon: img
},
events:{
click: function(marker, event, data){
$(this).gmap3({action:'addinfowindow', anchor:marker, options:{content: '<div id="content" style="width:300px;height:250px;"><img src="' + info + '"></img></div>'}});
/*var infowindow = $(this).gmap3({action:'get', name:'infowindow'});
infowindow.close();*/
},
}
});
}, i* 100);
}
for (var i = 0; i < markers.length; i++) {
var lat = markers[i][0];
var lng = markers[i][1];
var img = markers[i][2];
var info = markers[i][3];
configureTimeout(i, lat, lng, img, info);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With