IMOでは、リモートデータソースを使用するときに遅延をゼロに設定することはお勧めできません。必要以上に多くのリクエストが送信され、サーバーには何のメリットもありません。
とにかく、あなた自身がコールバックとしてsource
オプションを定義することで、あなたが望むものを達成できると思います。
最初に少しの説明。私はあなたがプラグインのソースとしてURLを渡すリモート機能を使用していると思います。プラグインは、実際にこの方法を実装するコールバックにこれをラップ:
// in case the option "source" is a string
url = this.options.source;
this.source = function(request, response) {
if (self.xhr) {
self.xhr.abort();
}
self.xhr = $.ajax({
url: url,
data: request,
dataType: "json",
autocompleteRequest: ++requestIndex,
success: function(data, status) {
if (this.autocompleteRequest === requestIndex) {
response(data);
}
},
error: function() {
if (this.autocompleteRequest === requestIndex) {
response([]);
}
}
});
};
あなたが見ることができるように、起こってAJAXリクエストは、それはそれをabordsすでに存在する場合。このようなご要望にお応えするには、ご利用のサーバーができるだけ早く、の時間がになり、遅延がゼロになります。
if (self.xhr) {
self.xhr.abort();
}
これは実際には、空のデータセットにそれ自体response
コールバックを実行する中断された要求のエラーコールバックを実行します。あなたが応答コールバックを見れば、データが空の場合、それはメニューを閉じます。
_response: function(content) {
if (!this.options.disabled && content && content.length) {
...
} else {
this.close();
}
あなたが実際にあなたのAJAX要求を自分で作ると、保留中のを中断しないことにより、デフォルトの動作を変更するために、独自のsource
コールバックを定義することができます要求。次のようなもの:
$('#autocomplete').autocomplete({
source: function(request, response) {
$.ajax({
url: url,
data: request,
dataType: "json",
success: function(data, status) {
// display menu with received dataset
response(data);
},
error: function() {
// close the menu on error by executing the response
// callback with an empty dataset
response([]);
}
});
}
});