js全局变量在回调函数中赋值后,无法正常取得 20C

var msg_sb="<%=request.getAttribute("sb")%>";
var msg_sbTime="<%=request.getAttribute("sbTime")%>";
var msg_carNum="<%=request.getAttribute("carNum")%>";
var msg_driver="<%=request.getAttribute("driver")%>";
var map = new BMap.Map("allmap");
var point = new BMap.Point(121.48054,31.236423);
//map.centerAndZoom(point, 15);
map.enableScrollWheelZoom(); 
map.addControl(new BMap.ScaleControl()); 

var pointStr = msg_sb.split(",");
var timeStr = msg_sbTime.split("@");
var pointArr = [];
var timeArr = [];
var locationArr = [];
var locationInfo = "";
var start = 0;
for (var k = 0; k < pointStr.length-1; k += 2) {
    pointArr.push({
        lng: pointStr[k],
        lat: pointStr[k + 1]
    });

}

for (var k = 0; k < timeStr.length-1; k += 1) {
    timeArr.push({
        time: timeStr[k]
    });
}

// 生成坐标点
var trackPoint = [];
for (var i = 0, j = pointArr.length; i < j; i++) {
    trackPoint.push(new BMap.Point(pointArr[i].lng, pointArr[i].lat));
}


getlocal (start)

function getlocal (i){
    if(i == pointStr.length-1){
        alert(i)
        setInfo();
       return
      }
    var locatedPoint = new BMap.Point(pointStr[i],pointStr[i + 1]);
    var gc = new BMap.Geocoder();
    gc.getLocation(locatedPoint, function(rs){
        var addComp = rs.addressComponents;
        if(addComp.province !=""){
            locationInfo = addComp.province;
        }
        if(addComp.city !=""){
            locationInfo += "," + addComp.city;
        }
        if(addComp.district !=""){
            locationInfo += "," + addComp.district;
        }
        if(addComp.street !=""){
            locationInfo += "," + addComp.street;
        }
        if(addComp.streetNumber !=""){
            locationInfo += "," + addComp.streetNumber;
        }
        addLocation(locationInfo);
        locationInfo = "";
    });
    i+=2; getlocal(i)
}

function addLocation(content){
    locationArr.push(content);
}

function  load() {}



//配置图片


map.addEventListener("click",function(e){
    var clickPoint = new BMap.Point(e.point.lng,e.point.lat);
    //var clickPoint = new BMap.Point(e.target.getPosition().lng,e.target.getPosition().lat);
    var gc = new BMap.Geocoder();
    gc.getLocation(clickPoint, function(rs){
        var addComp = rs.addressComponents;
        var clickedInfo = "";
        if(addComp.province !=""){
            clickedInfo = addComp.province;
        }
        if(addComp.city !=""){
            clickedInfo += "," + addComp.city;
        }
        if(addComp.district !=""){
            clickedInfo += "," + addComp.district;
        }
        if(addComp.street !=""){
            clickedInfo += "," + addComp.street;
        }
        if(addComp.streetNumber !=""){
            clickedInfo += "," + addComp.streetNumber;
        }
        var clickInfoWindow = new BMap.InfoWindow(clickedInfo,opts);  // 创建信息窗口对象 
        map.openInfoWindow(clickInfoWindow,clickPoint); //开启信息窗口  
    });
});
var icon;
var opts;
// 画图标
//window.onload = setTimeout(function(){
function setInfo(){
    icon = new BMap.Icon("./image/DAF_Tipper_48px_.png", 
            new BMap.Size(72,72),
            {imageOffset: new BMap.Size(0,0)});

    opts = {
              width : 200,
              height : 0,
              title : "</br>" ,
              enableMessage:true,//设置允许信息窗发送短息
              message:""
            } 

    for (var i = 0, j = trackPoint.length; i < j; i++) {
        if(i == 0){
            var marker = new BMap.Marker(trackPoint[i],{icon : icon}); 
            var content = "车牌:" + msg_carNum + "<br/>" + "司机:" + msg_driver + "<br/>" + "地址:"+ locationArr[i];
            var label = new BMap.Label(content,{offset:new BMap.Size(50,-30)});
            marker.setTop(true);
            map.addOverlay(marker);
            marker.setLabel(label);
        }else if(i == j - 1){
            var marker = new BMap.Marker(trackPoint[i],{icon : icon}); 
            var content = "车牌:" + msg_carNum + "<br/>" + "司机:"+ msg_driver + "<br/>" + "地址:"+ locationArr[i];
            var label = new BMap.Label(content,{offset:new BMap.Size(50,-30)});
            marker.setTop(true);
            map.addOverlay(marker);
            marker.setLabel(label);
        }else{
            var marker = new BMap.Marker(trackPoint[i]); 
            var content = "时间:" + timeArr[i].time + "<br/>" + "地址:"+ locationArr[i];
            map.addOverlay(marker);
            addClickHandler(content,marker);
        }
    }
    setZoom(pointArr)
}
//,3000);

//map.centerAndZoom(trackPoint[0], 15);

var polyline = new BMap.Polyline(trackPoint, {
    strokeColor: "#1869AD",//#1869AD
    strokeWeight: 6,
    strokeOpacity: 1
});
map.addOverlay(polyline);

function addClickHandler(content,marker){
    marker.addEventListener("mouseover",function(e){
        openInfo(content,e);
        }
    );
    /* marker.addEventListener("mouseout",function(e){
        setTimeout(map.closeInfoWindow(),5000);
        }
    ); */
}

function openInfo(content,e){
    var p = e.target;
    var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
    var infoWindow = new BMap.InfoWindow(content,opts);  
    map.openInfoWindow(infoWindow,point); 
}

function openInfo1(content,e){
    var p = e.target;
    var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
    var infoWindow = new BMap.InfoWindow(content,opts);  
    map.openInfoWindow(infoWindow,point);
}

//根据经纬极值计算绽放级别。
function getZoom(maxLng, minLng, maxLat, minLat) {
    var zoom = ["50", "100", "200", "500", "1000", "2000", "5000", "10000", "20000", "25000", "50000", "100000", "200000", "500000"]; // 级别18到3。
    var pointA = new BMap.Point(maxLng, maxLat); // 创建点坐标A
    var pointB = new BMap.Point(minLng, minLat); // 创建点坐标B
    var distance = map.getDistance(pointA, pointB).toFixed(1); //获取两点距离,保留小数点后两位
    for (var i = 0, zoomLen = zoom.length; i < zoomLen; i++) {
        if (zoom[i] - distance > 0) {
            return 18 - i + 5; 
        }
    }
}

function setZoom(points) {
    if (points.length > 0) {
        var maxLng = points[0].lng;
        var minLng = points[0].lng;
        var maxLat = points[0].lat;
        var minLat = points[0].lat;
        var res;
        for (var i = points.length - 1; i >= 0; i--) {
            res = points[i];
            if (res.lng > maxLng) maxLng = res.lng;
            if (res.lng < minLng) minLng = res.lng;
            if (res.lat > maxLat) maxLat = res.lat;
            if (res.lat < minLat) minLat = res.lat;
        }
        var cenLng = (parseFloat(maxLng) + parseFloat(minLng)) / 2;
        var cenLat = (parseFloat(maxLat) + parseFloat(minLat)) / 2;
        var zoom = getZoom(maxLng, minLng, maxLat, minLat);
        map.centerAndZoom(new BMap.Point(cenLng, cenLat), zoom);
    } else {
        map.centerAndZoom(new BMap.Point(103.388611, 35.563611), 5);
    }
}

请各位帮我看一下,代码里的全局变量locationArr,我在回调函数中赋值,然后在window.onload中取值,在chrome上是可以正常显示locationArr的值得,但是在ie上就不行,自己试了一些网上说的方法,document , ready,但是都不行,是在没办法了,大家能帮我找找原因吗?

3个回答

问题原因就是
getLocation没执行完 onload就触发了

粗略的写了下


var locationArr = []
 function  load {}

function  initLocation(){
    var pointlist = [1,2,3,4]
        var start = 0;
function  getlocal(i)
{
     if(start == pointlist.length){
         load();
        return
        }
    getLocation(list[i],  function(d){  locationArr.push(d);  start++; getlocal(start)}  )
}
}


Marthe
Marthe 谢谢回复,按照你的思路我修改了一下自己的代码(如上),但还是依然没有起作用,目前临时用了settimeout延时加载解决,但终究只是治标不治本
一年多之前 回复

楼上说的对
js代码要多注意这些时序问题
有的代码是异步执行的 比如ajax回调什么的 并不是严格按照你写的代码的顺序执行的

楼上说得对。把代码放入回调里面不就行了吗?觉得不好看用es6 promise(可能有兼容性问题)。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问