jQuery/Javascript - How to wait for manipulated DOM to update before proceeding with function

后端 未结 4 682
[愿得一人]
[愿得一人] 2020-11-30 07:16

What I\'m trying to do is to update a simple div to say \"Processing...\" before executing a CPU-intensive script (it takes 3-12 seconds to run, no AJAX) then update the div

4条回答
  •  青春惊慌失措
    2020-11-30 07:49

    As of 2019 one uses double requesAnimationFrame to skip a frame instead of creating a race condition using setTimeout.

    ....
    function doRun() {
        document.getElementById('msg').innerHTML = 'Processing JS...';
        requestAnimationFrame(() =>
        requestAnimationFrame(function(){
             start = new Date();
             end = addSecs(start,5);
             do {start = new Date();} while (end-start > 0);
             document.getElementById('msg').innerHTML = 'Finished Processing';   
        }))
    }
    ...
    

提交回复
热议问题