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,11 +22,15 @@ 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)
{
if (null == last_search) { // no request running
last_search = current_search;
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
xhr.open('GET', last_search, true); xhr.open('GET', last_search, true);
xhr.onload = function() { xhr.onload = function() {
@ -33,11 +38,19 @@ function search()
parts = JSON.parse(this.response); parts = JSON.parse(this.response);
results(); results();
} }
last_search = null;
if (next_search) {
do_search(next_search);
next_search = null;
}
}; };
xhr.onerror = function() { xhr.onerror = function() {
console.log("search call failed"); console.log("search call failed");
}; };
xhr.send(); xhr.send();
} else {
next_search = current_search; // save for next search
}
} }
function results() function results()