AJAX call freezes browser for a bit while it gets response and executes success

前端 未结 1 1931
渐次进展
渐次进展 2020-12-08 11:56

I am doing an AJAX call to my webserver which fetches a lot of data. i show a loading image that spins while the ajax call is executed and then fades away.

the thin

相关标签:
1条回答
  • 2020-12-08 12:48

    Since JavaScript is single threaded, a lot of sync processing will hang up the event queue and prevent other code from executing. In your case, it's the for-loop thats locking up the browser while it's executing.

    What you can try is putting all your iterations into your event queue.

    for (var i = 0 ; i < childNodes.length ; i = i + 1) {
        (function(i) {
            setTimeout(function(i) {
                // code-here
            }, 0)
        })(i)
    }
    

    This should space out the processing and not force the browser to finish them all at once. The self executing function is there to create a closure to hold on to the value of the loop counter i.

    0 讨论(0)
提交回复
热议问题