Agung Setiawan
Agung Setiawan

Reputation: 1164

jQuery Ajax only works once

i have this piece of code

$(document).ajaxStart($.blockUI).ajaxStop($.unblockUI);

it works as i expected unless it's only work once. After refreshing the page the above code run well but just like before works only once. Any suggestion how to fix it?

Upvotes: 0

Views: 983

Answers (1)

Micka
Micka

Reputation: 1838

Please read .ajaxStart documentation.

it is explained that:

Description: Register a handler to be called when the first Ajax request begins. This is an Ajax Event.

Whenever an Ajax request is about to be sent, jQuery checks whether there are any other outstanding Ajax requests. If none are in progress, jQuery triggers the ajaxStart event. Any and all handlers that have been registered with the .ajaxStart() method are executed at this time.

It means that if the first ajax request is not terminated and you send another one, this event is not fired again.

then, we can easily supposed that the behavior is the same for ajaxStop. Here is the documentation

Description: Register a handler to be called when all Ajax requests have completed. This is an Ajax Event.

Whenever an Ajax request completes, jQuery checks whether there are any other outstanding Ajax requests. If none remain, jQuery triggers the ajaxStop event. Any and all handlers that have been registered with the .ajaxStop() method are executed at this time. The ajaxStop event is also triggered if the last outstanding Ajax request is cancelled by returning false within the beforeSend callback function.

It means that as expected, this event is fired, only when all the currently running ajax request are terminated.

Or Maybe i didn't understand your problem ?

EDIT

You could also try with ajaxComplete(not enough rep to insert the link)

(Global Event) This event behaves the same as the complete event and will be triggered every time an Ajax request finishes.

this event will be fire regardless the ajax call succeed or failed. But it will also be called for each completed requests.

Upvotes: 1

Related Questions