gpt4 book ai didi

javascript - 关闭谷歌地图 infowindow.open(map) 异步行为

转载 作者:行者123 更新时间:2023-11-29 18:15:18 29 4
gpt4 key购买 nike

我有一个谷歌地图应用程序,点击 map 上的一个区域会创建信息窗口。

这是我的代码结构:

...
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(MapArea, "click", showTB);


function showTB () {
...
contentElement = //complicated table with buttons etc

infowindow.setContent(sContentString);
infowindow.setPosition(event.latLng);
infowindow.open(map);

//contentElement dom element manipulations, such as:
$("#table_row").css({background: "#8af"});
}

这里的问题是最后的 dom 元素操作通常不会执行。我的猜测是 infowindow.open(map) 是异步的,并且它通常只在 dom 元素操作部分之后才完成执行,由于 html 元素尚不存在,因此不会执行。在 infowindow.open(map) 之后放置一个 alert 语句会导致最后一段代码通常执行,假设是由于延迟造成的。

那么我如何确保任何代码仅在 infowindow.open() 执行完毕后执行?
我可以让它同步吗?还有另一种方法吗?我不只是想添加延迟功能,因为那样会非常慢或不可靠。

最佳答案

等待 infowindow 上的“domready”事件

domready | None | This event is fired when the containing the InfoWindow's content is attached to the DOM. You may wish to monitor this event if you are building out your info window content dynamically.

代码:

infowindow.setContent(sContentString);
infowindow.setPosition(event.latLng);
infowindow.open(map);
google.maps.event.addListener(infowindow,'domready', function() {
//contentElement dom element manipulations, such as:
$("#table_row").css({background: "#8af"});
});

关于javascript - 关闭谷歌地图 infowindow.open(map) 异步行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23810765/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com