I am writing a jQuery plugin that manipulates the value of an input field at the press of a button.
What I have so far is the ability to control the value by clicking the button, as well as to continually increase it if the user holds the button pressed. Simplified, the script is something like this:
var element = $('#test-input');
var interval;
$('#test-up-button').on({
mousedown : function(e) {
element.val(parseInt(element.val()) + 1);
//Wait 400ms, than do the interval
interval = window.setInterval(function() {
element.val(parseInt(element.val()) + 1);
}, 200);
e.preventDefault();
},
mouseup : function() {
window.clearInterval(interval);
}
});
(You can also see a working version here: )
However, as you can see in the ment, I also want when the mousedown event happens, after the initial increase of the value, the interval function to be delayed for 400ms, and only than to execute.
So that you press the button, value goes + 1, you hold the button a bit, and than the intervals start to roll.
I am writing a jQuery plugin that manipulates the value of an input field at the press of a button.
What I have so far is the ability to control the value by clicking the button, as well as to continually increase it if the user holds the button pressed. Simplified, the script is something like this:
var element = $('#test-input');
var interval;
$('#test-up-button').on({
mousedown : function(e) {
element.val(parseInt(element.val()) + 1);
//Wait 400ms, than do the interval
interval = window.setInterval(function() {
element.val(parseInt(element.val()) + 1);
}, 200);
e.preventDefault();
},
mouseup : function() {
window.clearInterval(interval);
}
});
(You can also see a working version here: http://jsfiddle/Husar/Hxhsh/#base )
However, as you can see in the ment, I also want when the mousedown event happens, after the initial increase of the value, the interval function to be delayed for 400ms, and only than to execute.
So that you press the button, value goes + 1, you hold the button a bit, and than the intervals start to roll.
Share Improve this question edited Nov 16, 2015 at 19:32 KatieK 13.9k19 gold badges78 silver badges91 bronze badges asked Apr 1, 2012 at 20:27 MaverickMaverick 2,0165 gold badges25 silver badges31 bronze badges 03 Answers
Reset to default 6Wrap the setInterval
in a setTimeout
. And, like interval
, keep and clear a timeout
value:
var interval, timeout;
// ...
timeout = window.setTimeout(function () {
interval = window.setInterval(function() {
element.val(parseInt(element.val()) + 1);
}, 200);
}, 400);
// ...
window.clearTimeout(timeout);
window.clearInterval(interval);
http://jsfiddle/coiscir/jUSg8/
Here you go: http://jsfiddle/Hxhsh/10/ :)
Just add a setTimeout
just use setTimeout to delay setInterval
mousedown: function(e) {
setTimeout(function() {
interval = window.setInterval(function() {
element.val(parseInt(element.val()) + 1);
}, 200);
}, 400);
e.preventDefault();
},
mouseup: function() {
window.clearTimeout(timeout);
window.clearInterval(interval);
}