Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 临时禁用“;viewchangeend“;事件_Javascript_Javascript Events_Bing Maps - Fatal编程技术网

Javascript 临时禁用“;viewchangeend“;事件

Javascript 临时禁用“;viewchangeend“;事件,javascript,javascript-events,bing-maps,Javascript,Javascript Events,Bing Maps,如何临时禁用viewchangeend事件,使用setView方法更改视图,然后再次重新启用viewchangeend事件 以下是我现在所做工作的一个较轻版本: var mapEventsObjects = []; /* Remove map events */ for ( var i = 0; i < mapEventsObjects.length; i++ ) { Microsoft.Maps.Events.removeHandler( mapEventsObjects[i]

如何临时禁用
viewchangeend
事件,使用
setView
方法更改视图,然后再次重新启用
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 );
var-mapEventsObjects=[];
/*删除映射事件*/
对于(变量i=0;i
我所期望的是在控制台中不会触发viewChangeEnd,但它不会出现一次,而是出现两次


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

您可以创建一个简单的布尔型gobal变量。如果要忽略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 )
});

您可以创建一个简单布尔值的gobal变量。如果要忽略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 )
});

我需要在调用
setView
之前临时禁用事件,然后重新启用它。由于JS的异步特性,如果我将该变量设置为true,然后调用
setView
并再次将其设置为false,则在调用
setView
时它将为false。不,它不会。在启动setView调用并且映射完成移动之前,它不会被设置为false。实际上,我以前已经多次使用此方法。您永远不会重新启用此事件。调用
setView
后,必须将
skipViewChangeEnd
设置为
true
,这样它将继续正常工作。但是如果您这样做,它将在
setView
完成之前设置为
true
,因此让事件再次激发。您只需要在下次调用setView时将其设置为true。skipviewchangeden变量在此之前应保持为false。您的代码示例仅显示调用setView一次。感谢后续评论。我用另一种方法解决了这个问题。我需要在调用
setView
之前暂时禁用事件,然后重新启用它。由于JS的异步特性,如果我将该变量设置为true,然后调用
setView
并再次将其设置为false,则在调用
setView
时它将为false。不,它不会。在启动setView调用并且映射完成移动之前,它不会被设置为false。实际上,我以前已经多次使用此方法。您永远不会重新启用此事件。调用
setView
后,必须将
skipViewChangeEnd
设置为
true
,这样它将继续正常工作。但是如果您这样做,它将在
setView
完成之前设置为
true
,因此让事件再次激发。您只需要在下次调用setView时将其设置为true。skipviewchangeden变量在此之前应保持为false。您的代码示例仅显示调用setView一次。感谢后续评论。我用另一种方法解决了这个问题。