Add a delay to the navigation typeahead to prevent many lookups

This commit is contained in:
Raphael Michel
2022-08-04 17:59:19 +02:00
parent 0bb41cc44e
commit e6c45e40a9

View File

@@ -12,13 +12,8 @@ $(function () {
var $query = $(this).find('[data-typeahead-query]').length ? $(this).find('[data-typeahead-query]') : $($(this).attr("data-typeahead-field")); var $query = $(this).find('[data-typeahead-query]').length ? $(this).find('[data-typeahead-query]') : $($(this).attr("data-typeahead-field"));
$container.find("li:not(.query-holder)").remove(); $container.find("li:not(.query-holder)").remove();
var lastQuery = ""; var lastQuery = "";
var runQueryTimeout = null;
$query.on("change", function () { function runQuery() {
if ($container.attr("data-typeahead-field") && $query.val() === "") {
$container.removeClass('focused');
$container.find("li:not(.query-holder)").remove();
return;
}
lastQuery = $query.val(); lastQuery = $query.val();
var thisQuery = $query.val(); var thisQuery = $query.val();
$.getJSON( $.getJSON(
@@ -119,6 +114,17 @@ $(function () {
$container.toggleClass('focused', $query.is(":focus") && $container.children().length > 0); $container.toggleClass('focused', $query.is(":focus") && $container.children().length > 0);
} }
); );
}
$query.on("change", function () {
if ($container.attr("data-typeahead-field") && $query.val() === "") {
$container.removeClass('focused');
$container.find("li:not(.query-holder)").remove();
return;
}
if (runQueryTimeout != null) {
window.clearTimeout(runQueryTimeout)
}
runQueryTimeout = window.setTimeout(runQuery, 250)
}); });
$query.on("keydown", function (event) { $query.on("keydown", function (event) {
var $selected = $container.find(".active"); var $selected = $container.find(".active");