From c35d3fd819e72e9fb93c3c303a1ee0fffc19d025 Mon Sep 17 00:00:00 2001 From: Tony Wasserka Date: Tue, 1 Oct 2024 17:14:47 +0200 Subject: [PATCH] Debounce search inputs --- assets/search.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/assets/search.js b/assets/search.js index 91add6fc..e98674df 100644 --- a/assets/search.js +++ b/assets/search.js @@ -32,7 +32,7 @@ } input.addEventListener('focus', init); - input.addEventListener('keyup', search); + input.addEventListener('input', search); document.addEventListener('keypress', focusSearchFieldOnKeyPress); @@ -79,7 +79,13 @@ .then(search); } + let timeoutId; function search() { + clearTimeout(timeoutId); + timeoutId = setTimeout(runSearch, 350); + } + + function runSearch() { while (results.firstChild) { results.removeChild(results.firstChild); }