Loop is more important than rest?

后端 未结 4 1722
旧巷少年郎
旧巷少年郎 2021-01-14 07:12

I want to execute simple code when user click on my button:

  1. First: change my cursor to \'wait\'
  2. Next: execute loop
  3. When loop is finished: cha
4条回答
  •  时光取名叫无心
    2021-01-14 07:59

    RequestAnimationFrame Way

    jsFiddle here

    (function (W) {
        W.onload = function () {
            var D = W.document,
                a = 0,
                c = D.getElementById('progress');
    
            function b() {
                c.innerText = a + 1;
                a++;
                if (a < 500) {
                    requestAnimationFrame(b);
                } else {
                    D.body.style.cursor = 'default';
                }
            }
    
            function start() {
                D.body.style.cursor = 'wait';
                b()
            }
            D.getElementById('gogogo').onclick = start;
        }
    })(window)
    

    This way you use less resources and so your complex link modification does not slow down other open websites.

提交回复
热议问题