DOM refresh on long running function

后端 未结 12 2371
醉话见心
醉话见心 2020-11-27 20:54

I have a button which runs a long running function when it\'s clicked. Now, while the function is running, I want to change the button text, but I\'m having problems in some

12条回答
  •  不知归路
    2020-11-27 21:24

    Slightly modified your code at jsfiddle and:

    $("#btn").on("click", dowork);
    
    function dowork() {
        document.getElementById("btn").innerHTML = "working";
        setTimeout(function() {
            for (var i = 1; i<1000000000; i++) {
                //
            }
            document.getElementById("btn").innerHTML = "done!";
        }, 100);
    }
    

    Timeout set to more reasonable value 100ms did the trick for me. Try it. Try adjusting the latency to find the best value.

提交回复
热议问题