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
You can get the timestamp when you detect the click on the div thanks to mousedown
. Similarly you can get the timestamp when you detect the click release thanks to mouseup
.
You then need to compare these two timestamps, if they are greater than 2 seconds (or 2000milliseconds) then you execute your function.
I know this question is pretty old now, but I was looking for something like this and found that Buley's answer was close to what I needed. I figured I'd post what I've done with it to have it function without the mouse up, sort of like an Android longtouch.
// Set the duration of the long press and declare a couple variables
var longpress = 1000;
var start;
var divMouseDown;
// Check for mousedown on the element of choice
$("#element").on('mousedown', function(e){
// Get the time it was clicked
start = new Date().getTime();
// See if mouse is still being held down after the longpress duration
divMouseDown = setTimeout(function(){
// What we want to happen when mouse is clicked and held for 1 second
}, longpress);
// If the mouse leaves the element or is released before the long touch event,
// reset variables and stop the function from triggering
$("#element").on('mouseup mouseleave', function(){
if (divMouseDown) {
clearTimeout(divMouseDown);
}
start = 0;
e.stopPropagation();
} );
} );
Just watch both mousedown
and mouseup
and calculate the difference. Here's an example.
(function() {
// how many milliseconds is a long press?
var longpress = 3000;
// holds the start time
var start;
jQuery( "#pressme" ).on( 'mousedown', function( e ) {
start = new Date().getTime();
} );
jQuery( "#pressme" ).on( 'mouseleave', function( e ) {
start = 0;
} );
jQuery( "#pressme" ).on( 'mouseup', function( e ) {
if ( new Date().getTime() >= ( start + longpress ) ) {
alert('long press!');
} else {
alert('short press!');
}
} );
}());
In the two years since this question was asked an awesome plugin called jQuery Finger was invented:
http://ngryman.sh/jquery.finger/
$('div').on('press', function(e) {
console.log(this, e);
});
This solution executes the action after the time you set. It also cancels the action if you leave the pressed element with the mouse.
var longpress={
pressed:false,
longpressed:false,
presstime:1000
};
$('#element').on( 'mousedown' , function( event ) {
longpress.longpressed=false;
longpress.pressed=true;
longpress.timeout=setTimeout(function() {
longpress.longpressed=true;
//Long press action here!
},longpress.presstime);
}).on( 'mouseup' , function( event ) {
clearTimeout(longpress.timeout);
if (!longpress.longpressed && longpress.pressed) {
longpress.pressed=false;
//Short press action here!
}
}).on('mouseleave', function( event ) {
clearTimeout(longpress.timeout);
longpress.pressed=false;
});
We can calculate the time difference when the mouse clicks and leave. We can get the timestamp
when you click and release a button
or div
using mousedown
and mouseup
Here's an example to detect the simple click and 2 seconds
long press. Here I have done with a button
press. Similarly, you can use div
$(document).ready(function () {
var pressTime; // store the press time
$("#pressHere").on('mouseup', function () {
var holdTime = 2000; //2 milliseconds for long press
if (new Date().getTime() >= (pressTime + holdTime))
$("#status").text("It's a Long Press...");
else
$("#status").text("It's a Short Press...");
}).on('mousedown', function () { // When Button Release
pressTime = new Date().getTime();
}).on('mouseleave', function () { // When Button Leave
pressTime = 0;
});
});
.input-bar-item {
display: table-cell;
}
.button-item {
margin: 20px;
width: 30%;
}
.width100 {
width: 60%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="input-bar-item button-item">
<button class="btn btn-info" id="pressHere">Press Me</button>
</div>
<div class="input-bar-item width100" id="status"></div>