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