Double Tap/ double click Angular2 & ionic
To catch the double click event, the following can be used:
(dblclick)="clickFunction()"
If we want to fire a function on click and onother function on double click we can use the following:
<button (click)="simpleClickFunction()" (dblclick)="doubleClickFunction()">click me!</button>
However, the simpleClickFunction
function will be called also when doubleClickFunction
is fired. To prevent it to happen, setTimeout
can help as the following:
html template
<button (click)="simpleClickFunction()" (dblclick)="doubleClickFunction()">click me!</button>
Component
simpleClickFunction(): void{
this.timer = 0;
this.preventSimpleClick = false;
let delay = 200;
this.timer = setTimeout(() => {
if(!this.preventSimpleClick){
//whatever you want with simple click go here
console.log("simple click");
}
}, delay);
}
doubleClickFunction(): void{
this.preventSimpleClick = true;
clearTimeout(this.timer);
//whatever you want with double click go here
console.log("double click");
}
html file
<button (tap)="tapEvent()">Tap Me!</button>
ts file
let count : number = 0;
tapEvent(){
this.count++;
setTimeout(() => {
if (this.count == 1) {
this.count = 0;
alert('Single Tap');
}if(this.count > 1){
this.count = 0;
alert('Double Tap');
}
}, 250);
}
So after 1-2 hours it was obvious, you don't need to catch double click events with Ionic, but with pure JavaScript: dblclick()
So in Angular 2 it would be: (dblclick)="myFunction()"
and that's it!
Here you will find other events for JavaScript.