简体   繁体   English

简单的javascript settimeout问题

[英]simple javascript settimeout problem

<script type="text/javascript">



$(document).ready(function () { initialize(); });

var markerarray = [
['Mount Evans', 39.58108, -105.63535, 4, 'This is Mount Evans.'],
['Badlands National Park', 43.785890, -101.90175, 1, 'This is Badlands National Park']
];


var bounds = new google.maps.LatLngBounds();

function setMarkers(map, markers) {

    for (var i = 0; i < markers.length; i++) {  setTimeout(function() {  

        var markerarray = markers[i];

        var siteLatLng = new google.maps.LatLng(markerarray[1], markerarray[2]);

        var marker = new google.maps.Marker({

            position: siteLatLng,

            map: map,

            animation: google.maps.Animation.DROP,

            title: markerarray[0],

            zIndex: markerarray[3],

            html: markerarray[4]

        }); 


        google.maps.event.addListener(marker, "click", function () {

            $('.info').html(this.html);

        });

        bounds.extend(siteLatLng);

        map.fitBounds(bounds);

    }  , i * 2000); } 

}


    function initialize() {

    var myOptions = {

        disableDefaultUI: true,

        mapTypeId: google.maps.MapTypeId.ROADMAP

    }

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    setMarkers(map, markerarray);

}

When i use the setTimeout(function() {... i get a javascript error: "markerarray is undefined". But when i remove the timeout everything work as it should. But i want a delay between each marker when theyre added to the map. Did i miss something? Thanks 当我使用setTimeout(function() {...我遇到了一个JavaScript错误:“ markerarray未定义”。但是当我删除超时时,一切都会正常进行。但是,当每个标记添加到地图,我想念什么吗?

This took a bit of figuring out but it is a very nice example of where someone can get caught. 这需要花点时间才能弄清楚,但这是一个很好的例子,说明有人会被抓到。 The issue lies with i not markerarray . 问题在于i不是markerarray

By the time that the setTimeout fires (after two seconds) the for loop has finished and i is set to 2. markers[i] is therefore markers[2] , which does not exist, and so markerarray (or markerarray2 , for clarity, in my example) is set to undefined . setTimeout触发时(两秒钟后), for循环已经完成并且i设置为2。因此, markers[2] markers[i]是不存在的markers[2] ,所以markerarray (或markerarray2 ,为清楚起见,在我的示例中)设置为undefined

The solution is to set up another variable, c in the example below. 解决方案是设置另一个变量,在下面的示例中为c That variable acts as your counter and so markerarray2 is defined because markers[c] is defined. 该变量充当您的计数器,因此定义了markerarray2 ,因为定义了markers[c]

var markerarray = [
['Mount Evans', 39.58108, -105.63535, 4, 'This is Mount Evans.'],
['Badlands National Park', 43.785890, -101.90175, 1, 'This is Badlands National Park']
];

function setMarkers(markers) {
    var c = 0;
    for (var i = 0; i < markers.length; i++) {  setTimeout(function() {  
            var markerarray2 = markers[c];
            c++;
            alert(markerarray2[0]);
        }, i * 1000);
    } 
}

setMarkers(markerarray);

我会尝试将MarkerArray定义移至第一行,然后再进行任何操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM