Reputation: 6296
i want to be able to call my function of setTimeout when a user enter a number in my input after 1s.
But the problem i want that this event is called just once for example when a user enter 3 numbers like 200 in a time less than 1s.
My code below is called 3 time if a user enter 3 numbers like 200
$('input.amount').on('input.amount',function(e){
setTimeout(function() {
// code
}, 1000);
});
So i want that this code is called just once
Upvotes: 2
Views: 57
Reputation: 388446
One approach I would take is to delay the execution till after 1000ms after the last input
var timer;
$('input.amount').on('input.amount', function (e) {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function () {
// code
timer = undefined;
}, 1000);
});
Upvotes: 1
Reputation: 103155
You may be able to keep a variable that tells you if the user already typed a character. Then only trigger the function if the variable is not yet set. For example:
var started = false;
$('input.amount').on('input',function(e){
if(!started){
started = true;
setTimeout(function() {
// code
started = false;//to reset so that further typing will trigger again
}, 1000);
}
});
Upvotes: 3