js: better handle multiple queries

This commit is contained in:
King Kévin 2023-02-26 12:11:57 +01:00
parent 4d891f17e7
commit feaee52376
1 changed files with 28 additions and 15 deletions

View File

@ -6,6 +6,7 @@
// the last search query // the last search query
var last_search = null; var last_search = null;
var next_search = null;
// the collection of parts // the collection of parts
var parts = null; var parts = null;
// last selected part // last selected part
@ -21,23 +22,35 @@ function search()
{ {
const terms = document.getElementById('terms'); const terms = document.getElementById('terms');
if (terms && terms.value && terms.value.length >= 3) { if (terms && terms.value && terms.value.length >= 3) {
} else { const current_search = '/search?terms=' + terms.value;
return; do_search(current_search);
} }
}
last_search = '/search?terms=' + terms.value; function do_search(current_search)
let xhr = new XMLHttpRequest(); {
xhr.open('GET', last_search, true); if (null == last_search) { // no request running
xhr.onload = function() { last_search = current_search;
if (decodeURI(this.responseURL).endsWith(last_search)) { let xhr = new XMLHttpRequest();
parts = JSON.parse(this.response); xhr.open('GET', last_search, true);
results(); xhr.onload = function() {
} if (decodeURI(this.responseURL).endsWith(last_search)) {
}; parts = JSON.parse(this.response);
xhr.onerror = function() { results();
console.log("search call failed"); }
}; last_search = null;
xhr.send(); if (next_search) {
do_search(next_search);
next_search = null;
}
};
xhr.onerror = function() {
console.log("search call failed");
};
xhr.send();
} else {
next_search = current_search; // save for next search
}
} }
function results() function results()