Will setInterval cause browsers to hang?

前端 未结 2 1621
耶瑟儿~
耶瑟儿~ 2020-11-28 15:14

A couple years ago I was warned against using setInterval for long periods of time as it supposedly would cause the browser to hang if the called function ran l

2条回答
  •  佛祖请我去吃肉
    2020-11-28 15:44

    it always better to use setTimeout in a loop so you know exactly when to continue timing:

    foo();
    function foo(){
    
       setTimeout (function(){
          foo = 'bar_' + i++;
          foo();
         }, 1 );
    
    } 
    

    otherwise as you said above, the browser will have to catch up and since ur loop is in infinitum, it might not.

提交回复
热议问题