Do not rely on timers in time

Source: Internet
Author: User

There are a lot of requirements that require timers, like the usual countdown, though it's simple, but there's a lot of bad implementations.

A timer can only provide the ability to trigger a queue at a certain point in time, and the point at which it is triggered is indeterminate.

For example, to increment or decrement a variable in a timer a behavior is not suitable for the countdown this implementation of the following code

var t =settinterval (function (){  --t;});

This code countdown is a big hole. If you switch the window to the countdown function, you're just a jerk.

Plus the triggering interval does not guarantee that if a longer message queue is plugged in, then your countdown will be postponed again!

So a countdown like this program is best based on time to calculate the following

var t = Date.now () +23333; Settinterval (function (_t){    if(Date.now ()-t>=0 )    //....});

In fact, we should use settimeout to replace setinterval setinterval can achieve settimeout can do.

Those animations that are non-CSS3 transition animate are the ones that depend on the timer where the real-time animation is manipulated by incrementing the decrement variable to be able to switch the window is also not much related and convenient.

But some features have to be calculated on a time-based basis!

For example, the upcoming react video barrage playback system The coordinate position of each projectile must be the F (t) function

This post doesn't have much to do with the above text (to improve the dream reach rate) ~

Do not rely on timers in time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.