Button function if the button is pressed for 10 seconds?
I am trying to find a way to execute a button function only if the user has pressed / pressed the button for 10 seconds.
normal button function:
$( "#target" ).click(function() {
alert( "Handler for .click() called." );
});
so is there a way to count the seconds from the moment the button is pressed by pressing the button and pressing KEPT (continuous press without pressing for not pressing), and when the seconds reach 10, the function will execute?
+3
source to share
5 answers
You should use mousedown and mouse up instead of click event,
var timeOut = 0;
$("#target").mousedown(function () {
clearTimeout(timeOut);
timeOut = setTimeout(function () {
console.log("Handler for .click() called.");
}, 10000);
});
$(document).mouseup(function () {
clearTimeout(timeOut);
console.log("stop");
})
0
source to share
Js
var threshold = 10000, timeOne, timeTwo;
$('#t10').mousedown(function(){
timeOne = new Date();
}).mouseup(function(){
timeTwo = new Date();
if(timeTwo - timeOne >= threshold){
alert('Do what you want');
timeOne = 0;
}else{
console.log('Released early');
}
});
Html
<button id="t10">XX</button>
Violins http://jsfiddle.net/5z94y2z5/
0
source to share
you need to use mousedown and mouseup events
http://api.jquery.com/mousedown/ http://api.jquery.com/mouseup/
var pressedtensecondos = false;
var timer = null;
$( "#target" ).mousedown(function() {
timer = setTimeout(function() { pressedtensecondos = true; }, 10000);
});
$( "#target" ).mouseup(function() {
clearTimeout(timer);
if(pressedtensecondos)
alert('ten seconds');
pressedtensecondos = false;
});
0
source to share