jquery::ajaxStop() 与 jquery::ajaxComplete()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4419241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery::ajaxStop() versus jquery::ajaxComplete()
提问by Chris
Which is to be used at which time.
哪个是在哪个时间使用。
In the documentation on http://api.jquery.com/:
在http://api.jquery.com/上的文档中:
For ajaxStop() it says:
对于 ajaxStop() 它说:
Description: Register a handler to be called when all Ajax requests have completed. This is an Ajax Event.
描述:注册一个处理程序,当所有 Ajax 请求完成时调用。这是一个 Ajax 事件。
And for the ajaxComplete() it says:
对于 ajaxComplete() 它说:
Description: Register a handler to be called when Ajax requests complete. This is an Ajax Event.
描述:注册一个在 Ajax 请求完成时调用的处理程序。这是一个 Ajax 事件。
From what I can see ajaxComplete() is more flexible due to:
从我可以看到的 ajaxComplete() 更灵活,因为:
All ajaxComplete handlers are invoked, regardless of what Ajax request was completed. If we must differentiate between the requests, we can use the parameters passed to the handler. Each time an ajaxComplete handler is executed, it is passed the event object, the XMLHttpRequest object, and the settings object that was used in the creation of the request.
无论完成什么 Ajax 请求,都会调用所有 ajaxComplete 处理程序。如果我们必须区分请求,我们可以使用传递给处理程序的参数。每次执行 ajaxComplete 处理程序时,都会传递事件对象、XMLHttpRequest 对象和创建请求时使用的设置对象。
Can someone explain what each is for and the appropriate usage for each. In an application I built recently I relied on ajaxStop() to fire when my ajax calls were finished. I would then parse the returned data for result of server side operation. Now I am starting to wonder if I should have used ajaxComplete() instead or a combination of both for various situations.
有人可以解释每个的用途以及每个的适当用法。在我最近构建的一个应用程序中,我依靠 ajaxStop() 在我的 ajax 调用完成时触发。然后我将解析返回的数据以获得服务器端操作的结果。现在我开始怀疑我是否应该在各种情况下使用 ajaxComplete() 或两者的组合。
Thoughts are appreciated.
想法表示赞赏。
回答by Nick Craver
Well, the short version is they serve different purposes, so the answer would be the "a combination of both for various situations" option. The basic rules are:
嗯,简短的版本是它们用于不同的目的,所以答案是“针对各种情况的组合”选项。基本规则是:
.ajaxComplete()
- runs for every requestthat completes, use this when you want to do something with each request/result. Note that this doesn't replace thesuccess
handler, since the parsed data is not one of the arguments (and it runs even when there's an error) - you may want.ajaxSuccess()
in some per-request situations instead..ajaxStop()
- runs when every batch of requestscompletes, usually you'd use this in combination with.ajaxStart()
for things like showing/hiding a "Loading..." indicator of some sort - or to do something else once a batch of AJAX requests finishes, like a master last step.
.ajaxComplete()
- 为每个完成的请求运行,当你想对每个请求/结果做一些事情时使用它。请注意,这不会替换success
处理程序,因为解析的数据不是参数之一(即使出现错误,它也会运行) - 您可能需要.ajaxSuccess()
在某些每个请求的情况下。.ajaxStop()
- 在每批请求完成时运行,通常您会将此与.ajaxStart()
诸如显示/隐藏某种“正在加载...”指示器之类的事情结合使用- 或者在一批 AJAX 请求完成后执行其他操作,例如高手最后一步。
If you're using this to parse your data, there's probably a better way, in this case $.ajaxSetup()
, where you can specify a success
handler that gets the already-parsed data (e.g. JSON responses will be objects), like this:
如果您使用它来解析您的数据,可能有更好的方法,在这种情况下$.ajaxSetup()
,您可以指定一个success
处理程序来获取已解析的数据(例如 JSON 响应将是对象),如下所示:
$.ajaxSetup({
success: function(data) {
//do something with data, for JSON it's already an object, etc.
}
});
回答by sje397
ajaxComplete
is called whenever an ajax request finishes, either successfully or with an error.
ajaxComplete
每当 ajax 请求完成时调用,无论是成功还是有错误。
ajaxStop
is called when allajax requests complete. So unlike ajaxComplete
, it won't be called if there is still a request in progress.
ajaxStop
当所有ajax 请求完成时调用。因此,与 不同的是ajaxComplete
,如果仍有正在进行的请求,则不会调用它。
You should use the first if the operation you want to perform should be done for every ajax request.
如果您要为每个 ajax 请求执行操作,您应该使用第一个。
回答by David Tang
In general, you want to use ajaxComplete
. This is because ajaxStop
will only be triggered when there are no more ajax requests still waiting to come back. This may not seem different when you're sending one ajax request at a time, but imagine that the network happened to slow down after you send request A and, when request B is sent 5 seconds later it comes back earlier than request A... then ajaxStop
will only be triggered once after request A returns while ajaxComplete
will fire both times.
通常,您希望使用ajaxComplete
. 这是因为ajaxStop
只有在没有更多 ajax 请求仍在等待返回时才会触发。当您一次发送一个 ajax 请求时,这似乎没有什么不同,但是想象一下,在您发送请求 A 后网络碰巧变慢了,并且当请求 B 在 5 秒后发送时,它比请求 A 更早返回。 . thenajaxStop
只会在请求 A 返回后触发一次,而ajaxComplete
两次都会触发。
The situation where you'd use ajaxStop
is when you send multiple ajax requests in one go... e.g. to submit a series of 3 forms... and want to be notified when all 3 complete successfully. Of course, you can achieve the same functionality with a counter within ajaxComplete
您使用的情况ajaxStop
是,当您一次性发送多个 ajax 请求时……例如,提交一系列 3 个表单……并且希望在所有 3 个表单成功完成时收到通知。当然,您可以使用计数器内的计数器来实现相同的功能ajaxComplete
However, from the sounds of your question... you'd like to parse data from each ajax response... typically this would be done in the ajax success callback, e.g.
然而,从你的问题的声音......你想从每个ajax响应中解析数据......通常这将在ajax成功回调中完成,例如
$.ajax({
url: "blah",
data: "blah",
success: function (data) { /* Code in here */ }
});