Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Added debounce on search input (#8)

ColinEspinasc636be90898f
diff --git a/mwmbl/tinysearchengine/static/index.js b/mwmbl/tinysearchengine/static/index.js
index 36d900f..a28dc3b 100644
--- a/mwmbl/tinysearchengine/static/index.js
+++ b/mwmbl/tinysearchengine/static/index.js
@@ -10,7 +10,7 @@ window.onload = (event) => {
const length = searchInput.value.length;
searchInput.setSelectionRange(length, length);
- searchInput.oninput = e => {
+ searchInput.oninput = debounce(e => {
console.log("Key", e.key);
console.log(searchInput.value);
@@ -27,7 +27,7 @@ window.onload = (event) => {
ts.numItems = content.length;
});
});
- };
+ });
// Handle moving the selected item up and down
document.addEventListener('keydown', (e) => {
@@ -57,6 +57,15 @@ window.onload = (event) => {
};
+function debounce(callback, timeout = 100){
+ let timer;
+ return (...args) => {
+ clearTimeout(timer);
+ timer = setTimeout(() => { callback.apply(this, args); }, timeout);
+ };
+}
+
+
function selectNextItem() {
if (ts.selected === null) {
ts.selected = 0;