From e6c45e40a9c5176477e7841696b2e9cc3922800f Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Thu, 4 Aug 2022 17:59:19 +0200 Subject: [PATCH] Add a delay to the navigation typeahead to prevent many lookups --- .../static/pretixcontrol/js/ui/typeahead.js | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/pretix/static/pretixcontrol/js/ui/typeahead.js b/src/pretix/static/pretixcontrol/js/ui/typeahead.js index 1e10f53ca5..b3067706f0 100644 --- a/src/pretix/static/pretixcontrol/js/ui/typeahead.js +++ b/src/pretix/static/pretixcontrol/js/ui/typeahead.js @@ -12,13 +12,8 @@ $(function () { 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(); var lastQuery = ""; - - $query.on("change", function () { - if ($container.attr("data-typeahead-field") && $query.val() === "") { - $container.removeClass('focused'); - $container.find("li:not(.query-holder)").remove(); - return; - } + var runQueryTimeout = null; + function runQuery() { lastQuery = $query.val(); var thisQuery = $query.val(); $.getJSON( @@ -119,6 +114,17 @@ $(function () { $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) { var $selected = $container.find(".active");