JS hangs on a do while loop

后端 未结 6 1492
南旧
南旧 2020-12-20 07:40

I am wondering how I can solve a hanging page with JS.

I have a JS loop which I\'m testing like this, but it seems to hang forever - is there a way to stop it hangin

6条回答
  •  囚心锁ツ
    2020-12-20 08:29

    You are concatenating 10,000,000 consecutive numbers together into a string, which really will not work well on any modern supercomputer.

    If you'd like to poll the progress, setup a timer to do it somewhat slower. It isn't practical, but it looks nice: http://jsfiddle.net/V6jjT/2/

    HTML:

    Script:

    var value = 0;
    var interval = setInterval(function() {
        if (value > 100) {
            clearInterval(interval);
            return;
        }
    
        value++;
        document.getElementById('my_data').innerHTML += ' ' + value;
    }, 10);
    

提交回复
热议问题