Focusing more on the execution part of the function while pressed:
Without a Interval
var count = 0;
var mainFunction = function(){
console.log(++count);
}
$(document).keydown(function(e){
e.preventDefault();
if(e.shiftKey) mainFunction();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Pressione shift
With a Interval
// mainFunction será sua função executada
var count = 0;
var mainFunction = function() {
console.log(++count);
}
var execFunction; //interval com a função
$(document).keydown(function(e) {
e.preventDefault();
if (e.shiftKey) {
clearInterval(execFunction);
execFunction = setInterval(function() {
mainFunction();
}, 10);
}
})
$(document).keyup(function(e) {
e.preventDefault();
clearInterval(execFunction);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>O contador irá ser executado enquanto a tecla shift estiver sendo pressionada.</h2>
Depending on how fast you want your function to run, I recommend using setInterval
, because if you pay attention the call is but fast. Besides of course, you can adjust the delay.
Attention should be paid to the use of the event keydown
, instead of keyup
, since the repetition can only occur in this event.
I think this will work for me, while pressing the function function works, otherwise.
– abduzeedo