私はaddEventListener
を持っていましたが、これはクリック回数に応じて複数回起動されます。一度だけ実行するようにするにはどうすればいいですか?私はremoveEventListener
を使ってみましたが、全く動かなかったのです。addEventListenerのEventListenerを削除するには?
ご協力いただきありがとうございます。事前に感謝:)あなたは、次のようremoveEventListener
に関数を渡す必要があり
Provider.ts
addInfoWindow(marker,ListingID){
this.http.get(this.baseURI + '/infoWindow.php?id=' + ListingID)
.map(res => res.json())
.subscribe(Idata => {
let infoData = Idata;
let content = "<ion-item>" +
"<h2 style='color:red;'>" + infoData['0'].ListingTitle + "</h2>" +
"<p>" + infoData['0'].ListingDatePosted + ' ' + infoData['0'].ListingStartTime + "</p>" +
"<p> Salary: $" + infoData['0'].ListingSalary + "</p>" +
'<p id="' + infoData['0'].ListingID + '">Apply</p>'
"</ion-item>";
console.log("CREATING INFOWINDOW WITH CONTENT");
let infoWindow = new google.maps.InfoWindow({
content: content
});
google.maps.event.addListener(infoWindow, 'domready',() => {
let goInfoPage = document.getElementById(infoData['0'].ListingID);
console.log("GETTING PAGE ID");
//let it run only once, currently will increment by 1 if infoWindow if closed and reopen
goInfoPage.addEventListener('click',() => {
let pushData = infoData['0'];
console.log("GETTING PAGE DATA");
let nav = this.app.getActiveNav();
console.log("PUSHHING TO PAGE");
nav.push('StoreInfoPage',{'record':pushData});
})
goInfoPage.removeEventListener('click',() => {
console.log("REMOVE EVENT LISTENER"); // did not run
});
});
google.maps.event.addListener(marker, 'click',() => {
console.log("MARKER CLICKED, OPENING INFOWINDOW");
infoWindow.open(this.map, marker);
});
});
}
正確に何をしたいですか? 'addEventListener'のコールバックの中で' removeEventListener'を呼び出して、 'addEventListener'で使用した関数への参照を渡してください –