gpt4 book ai didi

javascript - 暂时禁用 "viewchangeend"事件

转载 作者:行者123 更新时间:2023-11-28 06:51:15 27 4
gpt4 key购买 nike

如何暂时禁用 viewchangeend 事件,使用 setView 方法更改 View 并再次重新启用 viewchangeend 事件?

这是我现在正在做的事情的简化版本:

var mapEventsObjects = [];

/* Remove map events */
for ( var i = 0; i < mapEventsObjects.length; i++ ) {
Microsoft.Maps.Events.removeHandler( mapEventsObjects[i] );
}

/* Change the view */
map.setView({
"zoom": zoom,
"center": new Microsoft.Maps.Location( lat, long )
});

/* Add map events back */
var mapViewChangeEnd = Microsoft.Maps.Events.addHandler(map, "viewchangeend", function (e) {
console.log("viewChangeEnd Fired: ", e );
});

mapEventsObjects.push( mapViewChangeEnd );

我期望在控制台中看不到 viewChangeEnd Fired,但它出现的不是一次而是两次。

知道为什么它不起作用吗?

最佳答案

您可以创建一个简单 bool 值的全局变量。当您想忽略 viewchangeend 事件时,请将变量设置为 true。然后在 viewchangeend 事件的事件处理程序中,您可以进行简单的检查,如果该变量为 true,则将该变量设置为 false 并离开该函数。这是代码的修改版本:

var mapEventsObjects = [], skipViewChangeEnd = false;

//Add view change end event to map.
var mapViewChangeEnd = Microsoft.Maps.Events.addHandler(map, "viewchangeend", function (e) {
//Check to see if you want to skip this event once.
if(skipViewChangeEnd){
skipViewChangeEnd = false;
console.log("viewChangeEnd skipped");
}else{
//Your logic for handling this event
console.log("viewChangeEnd Fired: ", e );
}
});

mapEventsObjects.push( mapViewChangeEnd );

skipViewChangeEnd = true;

/* Change the view */
map.setView({
"zoom": zoom,
"center": new Microsoft.Maps.Location( lat, long )
});

关于javascript - 暂时禁用 "viewchangeend"事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32946709/

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