如何“禁用”运行一次后的javascript函数 [英] how to "disable" javascript function after being ran once
问题描述
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屋!