需求:延遲查詢,autocomplete延遲觸發執行事件。當有下一個事件開始時,本次事件中斷。目的是爲了防止調用服務器過於頻繁。ajax
var timeout = 0;//延時處理 $("#customerName").autocomplete({ minLength: 0, source: function (request, response) { clearTimeout(timeout); console.log(timeout); var customerName = $("#customerName").val(); var cutomerToken = $("#cutomerToken").val(); timeout = setTimeout(function () { console.log("run"); $.ajax({ url: "api/pmstransaction/getcustomerbybigdata", dataType: "json", data: { customerName: customerName, token: cutomerToken }, success: function (data) { response(data.Data); } }); }, 800); }, focus: function (event, ui) { $("#customerName").val(ui.item.Name); return false; }, select: function (event, ui) { module.viewModel.customerInfo.CustomerName(ui.item.Name); module.viewModel.customerInfo.CustomerTaxCode(ui.item.TaxCode); module.viewModel.customerInfo.CustomerAddressPhone(ui.item.AddressPhone); module.viewModel.customerInfo.CustomerBankAccount(ui.item.BankAccount); ); return false; } }).autocomplete("instance")._renderItem = function (ul, item) { return $("<li>") .append("<div>" + item.Name + "</div>") .appendTo(ul); }