I am trying to create something like an onFinishTyping
event which sets a timeout of 3 seconds. If the user writes something during these 3 seconds, I need to destroy that timer and set a new one. The problem is that after each button click, the event is fired.
Here is what I have:
//setup before functions
const jq = jQuery.noConflict();
var typingTimer; // Timer identifier.
var doneTypingInterval = 3000; // Time in ms.
jq(document).ready(function() {
// On keyup, start the countdown.
jq('.table-search-field').keyup(function(event) {
var typingTimer = setTimeout(function() {
doneTyping(event.target);
}, doneTypingInterval);
});
// On keydown, clear the countdown.
jq('.table-search-field').keydown(function() {
clearTimeout(typingTimer);
});
});
// User has “finished typing”, do something.
function doneTyping(field) {
var value = jq(field).val().toLowerCase(); // lower case
jq.ajax('index.php?option=com_mycomponent&task=player.search&keyword=' + value)
.done(function(data) {
console.log('ok');
}).fail(function() {
console.log('fail');
});
};