Is there a more accurate way to create a Javascript timer than setTimeout?

后端 未结 16 2147
礼貌的吻别
礼貌的吻别 2020-11-22 14:11

Something that has always bugged me is how unpredictable the setTimeout() method in Javascript is.

In my experience, the timer is horribly inaccurate in

16条回答
  •  攒了一身酷
    2020-11-22 14:43

    shog9's answer is pretty much what I'd say, although I'd add the following about UI animation/events:

    If you've got a box that's supposed to slide onto the screen, expand downwards, then fade in its contents, don't try to make all three events separate with delays timed to make them fire one after another - use callbacks, so once the first event is done sliding it calls the expander, once that's done it calls the fader. jQuery can do it easily, and I'm sure other libraries can as well.

提交回复
热议问题