[英]jQuery AJAX Calls - Is it possible to cancel unwanted calls?
I just wanted to ask.我只是想问问。 I have text input to allow users to type their city.
我有文本输入以允许用户输入他们的城市。
jQuery has a keyup
event linked to this input and sent an AJAX call after each character change. jQuery 有一个
keyup
事件链接到此输入,并在每次字符更改后发送 AJAX 调用。
I find however many unrequired AJAX calls are sent as the person is typing and depending on the number of results this can slow down performance here.然而,我发现许多不需要的 AJAX 呼叫是在该人打字时发送的,并且根据结果的数量,这可能会降低此处的性能。
Is there a way to cancel AJAX calls if a new call is made?如果拨打新电话,有没有办法取消 AJAX 电话? Is there a better way to do this?
有一个更好的方法吗?
An example can be seen here: www.datingjapan.co可以在这里看到一个例子: www.datingjapan.co
You can use the abort
method of the jqXHR
object returned by the ajax
method (other AJAX methods like get
and post
return a jqXHR
instance too):您可以使用
ajax
方法返回的jqXHR
object 的abort
方法(其他 AJAX 方法,如get
和post
也返回一个jqXHR
实例):
var xhr = $.ajax(opts);
xhr.abort();
Note that the jqXHR
object is effectively just an extension of XMLHttpRequest
, and XMLHttpRequest
itself also has an abort
method.请注意,
jqXHR
object 实际上只是XMLHttpRequest
XMLHttpRequest
也有一个abort
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.