Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle "single click" and "double click" on the same html DOM element using typescript:Angular 2 or 4?

Tags:

angular

My issue is, the methods used for both the events is getting triggered when I perform "double click"

For example, I need to perform specific functionality when specific event is triggered.

<a (click)="method1()" (dblclick)="method2()"> 

Both method1() and method2() are getting triggered when I perform "double click".

like image 620
Durgaachandrakala.E Avatar asked Jan 17 '18 06:01

Durgaachandrakala.E


People also ask

How do you stop a single click event when double clicking?

on('click',function(e){ if(e. originalEvent. detail > 1){ return; /* if you are returning a value from this function then return false or cancel the event some other way */ } }); Done.

How angular double click is implemented?

AngularJS ng-dblclick Directive The ng-dblclick directive tells AngularJS what to do when an HTML element is double-clicked. The ng-dblclick directive from AngularJS will not override the element's original ondblclick event, both are executed.

How do I double click in JavaScript?

The dblclick event generates an event on double click the element. The event fires when an element is clicked twice in a very short span of time. We can also use the JavaScript's addEventListener() method to fire the double click event. In HTML, we can use the ondblclick attribute to create a double click event.


1 Answers

You can use a timeout and a boolean flag to solve this. Consider the following:

The DOM takes a few milliseconds to recognize the double click.

But it's damn sure that it recognize the double click but the first click is also recognized.

So the logic goes like this.

isSingleClick: Boolean = true;       method1CallForClick(){    this.isSingleClick = true;         setTimeout(()=>{             if(this.isSingleClick){                  doTheStuffHere();             }          },250) } method2CallForDblClick(){          this.isSingleClick = false;          doTheStuffDblClickHere(); } 

Call the method one in the click event of the element and method 2 in the click event of the element.

like image 109
PranavKAndro Avatar answered Sep 21 '22 08:09

PranavKAndro