I would like to execute some function when the user presses for 2 seconds on a div.
Is it possible ?
Here is my code to detect the click on the
So this is obviously a long way in the future, but for people who want to do this still, a super simple and pretty elegant way to do this is by combining the clearTimeout function with mouseup / mouseout events:
$('#button').mousedown(function(){
var timer = setTimeout(
() => {alert('long press occurred');}, 600
);
}).mouseup(function(){
clearTimeout(timer);
}).mouseout(function(){
clearTimeout(timer);
});
#button {
width: 50px;
height: 50px;
background-color: blue;
color: white;
display: flex;
flex-direction: column;
text-align: center;
justify-content: center;
}