Last active
March 21, 2023 19:31
-
-
Save sukhikh18/0af4631832e52e921a444b5cb223cef1 to your computer and use it in GitHub Desktop.
Скрипт выполняющий функцию с задержкой после ввода #JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const keyupDebounce = function(input, callback, doneTypingInterval = 500) { // interval in ms | |
if (!input) return false; | |
let typingTimeoutTimer; // timer identifier | |
// Start the countdown on input/keyup | |
input.addEventListener('input', e => { | |
clearTimeout(typingTimeoutTimer) | |
// user is "finished typing" do something | |
typingTimeoutTimer = setTimeout(() => callback(), doneTypingInterval) | |
}); | |
// Reset the countdown on keydown | |
input.addEventListener('keydown', e => clearTimeout(typingTimeoutTimer)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment