I'm trying to implement two different functionalities to an element, one after a single click and another one after two clicks. I'm using a setTimeout to detect when a single click is performed, but this way both functions end up being performed, because the setTimeout continues counting, so I had the idea of stopping the time as soon as the second click is performed, but after that the setTimeout stops working and everything is seen as a double click, so I modeled everything until it looked like this
function howManyClicks(element){
var time;
let realization = new Promise((resolve)=>{
time = setTimeout(()=>{
resolve(1);
element.removeEventListener('click', dobleClick);
}, 200);
element.addEventListener('click', dobleClick);
function dobleClick(){
element.removeEventListener('click', dobleClick);
clearTimeout(time);
resolve(2);
}
});
realization.then((value)=>{
return value;
});
}
I think there are already enough non-functional and unnecessary things there, but they were my attempts to reach that goal. Does anyone know how to resolve?