How to call a function on jQuery Ajax timeout

十年热恋 提交于 2021-01-29 05:56:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!