-1
Good afternoon, I’m trying to do a function to perform an action while keeping an item from a list pressed pro x minutes, but I’m not finding a solution to this. Does anyone have any idea how I can do that?
-1
Good afternoon, I’m trying to do a function to perform an action while keeping an item from a list pressed pro x minutes, but I’m not finding a solution to this. Does anyone have any idea how I can do that?
3
You can do the following:
$( "#target" ).on({
mousedown: function() {
$(this).data('timer', setTimeout(function() {
foo();
}, 60000));
},
mouseup: function() {
clearTimeout( $(this).data('timer') );
}
});
This 60000 in setTimeout is the amount in milliseconds you want it to stay pressed. In case I want him to press for 10 seconds it would be:
10 x 1000 = 10000
$( "#target" ).on({
mousedown: function() {
$(this).data('timer', setTimeout(function() {
foo();
}, 2000));
},
mouseup: function() {
clearTimeout( $(this).data('timer') );
}
});
function foo() {
alert('Yaay !')
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="target">Segure por 2 segundos</button>
Did you know that it is possible to add HTML, CSS and JS codes to run in the answer itself? See how in this image.
I’ll take a look, thanks for the info :)
Thank you very much Vinicius, that’s what I needed.
+1 for editing with executable code in reply.
See the calculation (10 x 100 = 10000) is wrong.
corrected @Laérciolopes :) thanks
@Viniciusfarias How to do this with pure javascript?
Browser other questions tagged javascript jquery html5
You are not signed in. Login or sign up in order to post.
What have you tried? Show the code.
– MauroAlmeida
What is the jquery version?
– MauroAlmeida