问题
I am making an ajax call to a php file on my server.
That php file retrieves some info from various websites, so an ajax call can take 30 seconds to complete in the worst case.
All I want to do is to set a timeout value for the ajax call and to trigger a function when timeout is reached.
I think jQuery .complete is what I am looking for since it is called in any case but how to detect if it is called on timeout?
回答1:
Actually, you can use .error
for that instead of .complete
.
error(jqXHR, textStatus, errorThrown)
textStatus
will be "timeout".
回答2:
from http://api.jquery.com/jQuery.ajax/
complete(jqXHR, textStatus)
A function to be called when the request finishes (after success and error callbacks are executed). The function gets passed two arguments: The jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object and a string categorizing the status of the request ("success", "notmodified", "error", "timeout", "abort", or "parsererror").
来源:https://stackoverflow.com/questions/8691158/how-to-call-a-function-on-jquery-ajax-timeout