Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript variables undefined within setTimeout function

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?

like image 815
Rick Donohoe Avatar asked Oct 18 '12 16:10

Rick Donohoe


People also ask

What is setTimeout in JavaScript?

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:

How to pass a variable as a parameter to setTimeout?

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:

What is the difference between setTimeout and setInterval?

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.

Why is my setTimeout function not working?

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 ()


1 Answers

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);

}
like image 95
lanzz Avatar answered Nov 15 '22 14:11

lanzz