如何“禁用”运行一次后的javascript函数 [英] how to "disable" javascript function after being ran once

查看:105
本文介绍了如何“禁用”运行一次后的javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个html按钮,每个都运行不同的功能(两个功能都在下面)。基本上,您可以单击两个按钮中的一个来将Google Maps actionlistener添加到地图。我已经成功地将它工作。唯一的问题是我只希望actionlistener可以一键点击。之后,点击一次,我希望用户必须在actionlistener再次侦听之前点击另一个按钮。

  function addLaunch(){
google.maps.event.addListener(map, (
position:event.latLng,
map:map

});
infowindow.open(map,marker);
});
};
function addFishing(){
google.maps.event.addListener(map,click,function(event){
marker = new google.maps.Marker({
position:event.latLng,
map:map
});
fishinfowindow.open(map,marker);
});
};

所以我刚试过这个:

  function addLaunch(setBoolean){
var clicked = new Boolean(false);
clicked.boolValue = setBoolean;
if(clicked = true){
google.maps.event.addListener(map,click,function(event){
marker = new google.maps.Marker({
position:event.latLng,
map:map

});
infowindow.open(map,marker);
clicked.boolValue = false;
});
}
else {
google.maps.event.clearListeners(map,click);
}
};

并且它不工作.....请指向正确的方向...
(顺便说一下,按钮通过了true到'setBoolean'。



这可以在第一次点击后禁用所有的actionlisteners,但它不会

  var temp = true; 
function addLaunch(){
if(temp == true){
google.maps.event.addListener(map,click,function(event){
marker = new google.maps.Marker({
position :event.latLng,
map:map
$ b});
infowindow.open(map,marker);
temp = false;
if( temp == false){
google.maps.event.clearListeners(map,click);
}
});
}
}


解决方案

使用 google.maps.event.addListenerOnce ()



文档:


addListenerOnce(实例:对象,eventName:字符串,处理函数)
MapsEventListener与addListener类似,但处理完第一个事件后,处理程序会自行删除
。 / p>


So I have two html buttons that each run a different function (both functions are below). Basically, you click one of the two buttons to add a Google Maps actionlistener to the map. I've successfully got that to work. The only problem is that I only want the actionlistener to be available one click. After that one click I want the user to have to click another button before the actionlistener "listens" again. I hope that makes sense.

    function addLaunch() {
google.maps.event.addListener(map, "click", function(event) {
    marker = new google.maps.Marker({
      position: event.latLng,
      map: map

    });
    infowindow.open(map, marker);
});
};
function addFishing() {  
google.maps.event.addListener(map, "click", function(event) {
    marker = new google.maps.Marker({
      position: event.latLng,
      map: map
    });
    fishinfowindow.open(map, marker);
});
};

So I just tried this:

function addLaunch(setBoolean) {
var clicked = new Boolean(false);
    clicked.boolValue = setBoolean;
if (clicked = true) {
google.maps.event.addListener(map, "click", function(event) {
    marker = new google.maps.Marker({
      position: event.latLng,
      map: map

        });
        infowindow.open(map, marker);
        clicked.boolValue = false;
    });
   }
   else {
   google.maps.event.clearListeners(map, "click");
   }
 };

and it didn't work..... Please point me in the right direction... (BTW, the button passed "true" to the 'setBoolean'.

This works to disable all actionlisteners after the first click. But it doesn't reset after the button is clicked again.

    var temp = true;
function addLaunch() {
    if (temp == true) {
        google.maps.event.addListener(map, "click", function(event) {
    marker = new google.maps.Marker({
      position: event.latLng,
      map: map

        });
infowindow.open(map, marker);
        temp = false;
        if (temp == false) {
        google.maps.event.clearListeners(map, "click");
                }
        });
    }
}

解决方案

Use google.maps.event.addListenerOnce()

From the documentation:

addListenerOnce(instance:Object, eventName:string, handler:Function) MapsEventListener Like addListener, but the handler removes itself after handling the first event.

这篇关于如何“禁用”运行一次后的javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆