Debounce keyup events when searching for existing customers

This commit is contained in:
Alex Tselegidis 2020-12-09 16:00:15 +02:00
parent b794cb6e74
commit 53a6a8c3a8

View file

@ -223,11 +223,19 @@ window.BackendCalendarAppointmentsModal = window.BackendCalendarAppointmentsModa
$('#select-customer').trigger('click'); // Hide the list. $('#select-customer').trigger('click'); // Hide the list.
}); });
var filterExistingCustomersTimeout = null;
/** /**
* Event: Filter Existing Customers "Change" * Event: Filter Existing Customers "Change"
*/ */
$('#filter-existing-customers').on('keyup', function () { $('#filter-existing-customers').on('keyup', function () {
if (filterExistingCustomersTimeout) {
clearTimeout(filterExistingCustomersTimeout);
}
var key = $(this).val().toLowerCase(); var key = $(this).val().toLowerCase();
filterExistingCustomersTimeout = setTimeout(function() {
var $list = $('#existing-customers-list'); var $list = $('#existing-customers-list');
var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_customers'; var url = GlobalVariables.baseUrl + '/index.php/backend_api/ajax_filter_customers';
@ -237,6 +245,8 @@ window.BackendCalendarAppointmentsModal = window.BackendCalendarAppointmentsModa
key: key key: key
}; };
$('#loading').css('visibility', 'hidden');
// Try to get the updated customer list. // Try to get the updated customer list.
$.post(url, data) $.post(url, data)
.done(function (response) { .done(function (response) {
@ -280,7 +290,11 @@ window.BackendCalendarAppointmentsModal = window.BackendCalendarAppointmentsModa
.appendTo($list); .appendTo($list);
} }
}); });
})
.always(function() {
$('#loading').css('visibility', '');
}); });
}, 1000);
}); });
/** /**