How can I force the browser to redraw while my script is doing some heavy processing?

后端 未结 2 460
萌比男神i
萌比男神i 2020-12-11 16:47

I am using the innerHTML property to modify a DIV dynamically, to report on a process that takes a few seconds to finish. The problem is that on Fi

相关标签:
2条回答
  • 2020-12-11 17:25

    Try interrupting your script periodically. You should be able to use

    setTimeout(nextFunction, 0); 
    

    to provide the necessary interruption without a long delay, where nextFunction is the function that continues with your lengthy processing.

    0 讨论(0)
  • 2020-12-11 17:28

    The browser is single threaded. While the script is running, the browser can't do anything else. If you want to do something like a progress meter, you have to use setTimeout(), or setInterval(), and break your task down into smaller chunks that get run on an interval. This leaves gaps in between the script runs, giving control back to the browser, where the browser can redraw.

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