How to load an ajax (jquery) request response progressively without waiting for it to finish?

前端 未结 4 2056
走了就别回头了
走了就别回头了 2020-12-06 08:28

I want to make a form that will use jquery to submit a list of keyword to a php file, this file could take a lot of time to load depending on the size of the keywords list.<

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-06 08:45

    Updated: The most reliable way to use Ajax is to consider each Ajax request as being atomic: it works or it doesn't.

    And you (the JS running on the page) don't really get any feedback as your request is executing. So you'll need multiple calls. But be careful! Lots of overhead per call and most browsers will only execute one or maybe two at a time.

    I suggest using a busy indicator to the human. You could also show a count down clock that updates at least twice per second to make it seem like something is happening.

    Added:

    You may also want to re-architect your solution. Eg view the update process as a batch job that the Ajax kicks off by submitting the request. Then make further Ajax calls to learn (and then) display the progress of the batch job to the human.

    Comet and other "keep the pipe open strategies" can be used but are tough to get working reliably and tend to use a lot of resources. A more pedestrian polling technique can be a lot easier to get going. Then optimize if needed.

提交回复
热议问题