需求:延迟查询,autocomplete延迟触发执行事件。当有下一个事件开始时,本次事件中断。目的是为了防止调用服务器过于频繁。
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); }