chiro-canto/public/upload/scripts/ajax_species_suggestion.js

43 lines
1.3 KiB
JavaScript
Executable File

function showHint(str) {
if (str.length == 0) {
document.getElementById("hint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("hint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", `getspecieshint.php?q=${str}`, true);
xmlhttp.send();
}
}
document.getElementById('sp-entry').addEventListener('keyup', function() {
// console.log(this.value);
showHint(this.value);
});
function waitForElementToDisplay(selector, callback, checkFrequencyInMs, timeoutInMs) {
var startTimeInMs = Date.now();
(function loopSearch() {
if (document.querySelector(selector) != null) {
callback();
return;
} else {
setTimeout(function() {
if (timeoutInMs && Date.now() - startTimeInMs > timeoutInMs)
return;
loopSearch();
}, checkFrequencyInMs);
}
})();
}
// Call the below function
waitForElementToDisplay("#species-hint", function() {
this.addEventListener('click', function() {
species_select = document.getElementById('species');
});
}, 1000, 9000);