showing progressbar progress with ajax request

后端 未结 5 913
清歌不尽
清歌不尽 2020-12-01 06:55

I want to show progress with jquery ui progress bar when an ajax request fires and when it finishes. The problem is I don\'t know how to set values for the progress bar depe

5条回答
  •  星月不相逢
    2020-12-01 07:19

    I faced this situation in our company's in-house app. It was a critical app and we needed to show progress to the user. So, despite the fact that @Thilo mentioned about network traffic, we had to implement it so that the user could be informed when the long running process was finished and how it progressed over time.

    We broke it down to small pieces.

    1. Spawn a thread on the server-side code to run the particular process. For ASP.Net it was like this:

      System.Threading.ThreadPool.QueueUserWorkItem(AddressOf MyProcessRoutine, objectParameter)

    2. Within this "MyProcessRoutine" run your long-running code and update a static variable for counter or persist in database, for each step you want to monitor.

    3. Create another function routine which keeps checking this counter and returns the current value. For example: Public Shared Function CheckStatus() As Integer

    4. From your client-side code keep polling this routine say every 5 seconds (depending on how much network traffic you can afford and how much precision you want. For example: timer = setInterval("MyWebService.CheckStatus();", 500);

    5. In your client-side code, use this returned value (counter) to update your progress bar.

    Besides just a counter, you can also return an object containing relevant details like record-id to show the user about where the process is currently at.

    Make sure you handle the thread very carefully, handle all possible exceptions and have proper cleanup.

提交回复
热议问题