且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

显示进度条进度与ajax请求

更新时间:2022-04-14 03:56:02

基本问题是您不知道请求将花费多长时间.

The fundamental problem is that you do not know how long the request is going to take.

对于进度条,您需要设置百分比或完成的步骤数.

For the progress bar, you need to set a percentage, or a number of completed steps.

如果您不希望进度条从0跳到100,则需要某种方法来测量请求完成之前的完成率.

If you do not want the progress bar to just jump from 0 to 100, you need to have some way to measure the completion rate before the request is finished.

如果您可以猜测需要多长时间,则可以使用计时器将其自动递增至例如90%,并在服务器响应到来时将其设置为100%.当然,这真是太伪了.

If you can guess how long it takes, you could use a timer to have it incrementally advance to, say, 90%, automatically, and when the server response comes it, set it to 100%. Of course, that is faking it quite a bit.

如果您有多个请求,则可以使用已完成请求的数量作为百分比.如果可以的话,您可以将单个请求分解为多个请求,但请仔细考虑这对网络流量和响应时间的影响.不要只是为了进度栏而做.

If you have more than one request, you could use the number of completed requests as a percentage. If it makes sense, you could break down your single request into multiple, but carefully think about the impact this has on network traffic and response times. Don't just do it for the sake of the progress bar.

如果请求确实花费很长时间,则可以向服务器发出其他请求以查询进度.但这可能意味着需要在服务器端进行大量工作.

If the request really takes long, you could fire off additional requests to the server to inquire the progress. But that could mean a lot of work server-side.

对不起,但是进度条很难...

Sorry, but progress bars are hard...