setInterval() behaviour with 0 milliseconds in JavaScript

前端 未结 4 979
孤独总比滥情好
孤独总比滥情好 2020-11-27 21:18

In my application I found some JavaScript code that is using setInterval with 0 milliseconds, like so:

self.setInterval(\"myFunction()\",0);
         


        
4条回答
  •  没有蜡笔的小新
    2020-11-27 21:18

    Browser set a minimal value for the interval. Usualy 10ms, but it can depend on the browser. This means repeat this as fast as I'm possibly allowed. The W3C spec say 4ms : http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#timers

    This is correct but probably reveal a design error.

    EDIT: By the way, it is bad practice to pass a string to setTimeout/setInterval, pass a function instead as javascript has first class functions.

提交回复
热议问题