I need to execute one function on single click and other function on double click. Problem I am facing is due to the fact that we can not have double click event without first having two single clicks. I want, whenever we have double click then only double click function should execute not single click function . Below is something I have done so far. Please suggest if there is any better way to do it because below code is not always working perfectly.
HTML
<div class='box'>
JS:
var dblclick = false;
//single click function
$('div.box').on('click',function(){
setTimeout(function(){
if(!dblclick){
console.log('singleClick');
}
},200);
});
//resetting double click flag
$('div.box').on('click',function(){
setTimeout(function(){
dblclick = false;
},400);
});
//double click function
$('div.box').on('dblclick',function(){
dblclick = true;
console.log('doubleClick')
});
CSS:
div.box {
height: 100px;
width: 100px;
border: 1px solid black;
}