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
Simple. No need for long uneccesary coding.
(function(){
// how many milliseconds is a long press?
var offset = 500;
$(".button").on('mousedown', function(e){
// holds the start time
$(this).data('start',new Date().getTime());
$(this).addClass("selected");
}).on('mouseup', function(e){
if (new Date().getTime() >= ($(this).data('start') + offset)){
//alert('ur click lasted for over 2 secs');
$(this).addClass("selected");
}else{
$(this).removeClass("selected");
}
}).on('mouseleave', function(e){
start = 0;
//you can add destroy lingering functions on mouse leave
});
}());
http://jsfiddle.net/donddoug/8D4nJ/