Google Maps V3版本,容易的infowindow.close不成功

Google Maps V3版本,简单的infowindow.close不成功
var lastInfWnd = null;

google.maps.event.addListener(_marker, 'click', function() {
  var infowindow = new google.maps.InfoWindow({
  content: divMarkerInf
  });
  infowindow.open(map, _marker);
  if (lastInfWnd) {
  lastInfWnd.close();
   
  }
  lastInfWnd = infowindow;
   
  });

就是想点开新的infowindow的时候把上次打开的infowindow关掉。我发现它不成功的原因可能是因为这是个网络程序,有速度问题,鼠标点到marker的时候不执行lastInfWnd.close();,
或者当执行lastInfWnd.close();的时候,还没有关掉lastInfWnd,马上就又执行了lastInfWnd = infowindow;
所以lastInfWnd的值改了,lastInfWnd.close()当然关掉的也不是真正的上个弹窗。

我想到的办法是

google.maps.event.addListener(_marker, 'click', function() {
  var infowindow = new google.maps.InfoWindow({
  content: divMarkerInf
  });
  infowindow.open(map, _marker);
  if (lastInfWnd) {
  //lastInfWnd.close();
  setTimeout(function() { lastInfWnd.close(); }, 1000);

  }
  setTimeout(function() { lastInfWnd = infowindow; }, 3000);
   
  });

调试的时候一步一步,等的时间长,效果是正确的,但真正运行,又是错的,到底为什么呀

------解决方案--------------------
if (lastInfWnd) {
lastInfWnd.close();
 
}
var infowindow = new google.maps.InfoWindow({
content: divMarkerInf
});
infowindow.open(map, _marker);

这样的顺序,能拖延lastInfWnd.close();占用的时间