How long will the browser wait after an ajax request?

前端 未结 4 1424
醉话见心
醉话见心 2020-12-07 22:41

How long can the browser wait before an error is shown before server answers for request? Can this time be unlimited?

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 23:04

    Yes and no. Yes the server can do it or be configured to do so, no the browsers (i dont know about version/distributor specifics) may have timeouts enabled.

    There are 2 solutions though for achieving/emulating this over HTTP:

    • If this is simple a long running script and you're waiting for results this isnt the way to go, you should instead do as previous poster mentioned and use async processing with server polling for the results, this would be a much more sure fire solution. For example: a thumbnail script from an image processor server side: the user uploads an image, the server immediately returns a 200 and a "Job ID". The client (javascript^^) can then use the JobID to request the job status/result.
    • If your goal is to have something like a realtime connection between browser and server (1 way connection, once the request is made by the browser no further info can be sent without using new requests (ajax^^)), this is called long polling/reverse ajax and can be used for real-time communication over http. There are several techniques using 2 long polled requests in parallel so that once one of them timeout the second one becomes the active and the first one attempts to reconnect.

提交回复
热议问题