From 26f26f36cb7ae5b2f2f5377ff390f5f27da0f960 Mon Sep 17 00:00:00 2001 From: krateng Date: Thu, 16 Jan 2025 06:22:35 +0100 Subject: [PATCH] Add debounce timer to search, GH-370 --- maloja/web/static/js/search.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/maloja/web/static/js/search.js b/maloja/web/static/js/search.js index 1dd60d5..246479a 100644 --- a/maloja/web/static/js/search.js +++ b/maloja/web/static/js/search.js @@ -1,17 +1,23 @@ var searches = [] +var debounceTimer; function search(searchfield) { - txt = searchfield.value; - if (txt == "") { - reallyclear() - } - else { - xhttp = new XMLHttpRequest(); - searches.push(xhttp) - xhttp.onreadystatechange = searchresult - xhttp.open("GET","/api/search?max=5&query=" + encodeURIComponent(txt), true); - xhttp.send(); - } + clearTimeout(debounceTimer); + debounceTimer = setTimeout(() => { + const txt = searchfield.value; + if (txt == "") { + reallyclear(); + } + else { + const xhttp = new XMLHttpRequest(); + searches.push(xhttp); + xhttp.onreadystatechange = searchresult + xhttp.open("GET","/api/search?max=5&query=" + encodeURIComponent(txt), true); + xhttp.send(); + } + }, 1000); + + }