How to keep animated gif running while doing intense calculations

后端 未结 7 1504
一向
一向 2020-12-06 05:15

I have a page which does some intense and long lasting calculations in Javascript. I would like to have a loading animation to tell the user progress is being made. I have a

7条回答
  •  遥遥无期
    2020-12-06 05:54

    You probably don't need it anymore, but just in case anyone else is googling it, here's another simple solution:

    *note that it would only work if this huge script execution is being caused by a long iteration loop

    -make your loop something like this:

    function AsyncLoop(i, lastLoop, parameters) {
            setTimeout(function () {
                if (i < lastLoop) {
                    //whatever you wanna do here
                    window.console.log('iteration ' + i + ': ' + parameters[i].whatevs);
    
                    i++;
                    AsyncLoop(i, lastLoop, parameters);
                }
            }, 0);
        }
    

    -then call it like:

    AsyncLoop(0, incredblyLongObjectArray.length, incredblyLongObjectArray);
    

    Here's a little fiddle without using parameters (so it's easier to understand) and with a function to detect if the desirable script has finished executing:

    https://jsfiddle.net/or1mrmer/

提交回复
热议问题