Progress indicator freezes while script runs

Discussion in 'JavaScript' started by Sleepless, Feb 27, 2008.

  1. #1
    My Javascript page uses AJAX to fetch data, and then process and display it.

    I use one of those GIF progress indicators while the whole operation takes place (examples are here: http://swik.net/Ajax/Ajax+Images)

    As long as the script waits for the data, the progress indicator keeps turning, which is great. However, once the data arrives from the server and the script starts to process the data, the indicator freezes.

    I wonder if there's a way around that - for example, can Javascript suspend its calculation from time to time to let the browser do some more rendering.
     
    Sleepless, Feb 27, 2008 IP
  2. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You will need to get creative with the setTimeout() function. I've had to do this before. It won't necessarily work for every situation, but basically what you need to do is maintain the "state" of the long process using global variables. Have the main function which does the processing only run for a few iterations and then quit, using a setTimeout() statement to call itself again with a delay of zero ms as the last statement within the function. The 0ms delay gives the browser enough time to catch up and then it will immediately continue processing after it does.

    It's not pretty, but if you want to avoid the problem you'll need to do something like this. And to make matters worse, it won't necessarily be possible for every situation.
     
    vpguy, Feb 28, 2008 IP