DEV Community

Cover image for How to make an accurate and precise timer in JavaScript
Morten Vestergaard
Morten Vestergaard

Posted on

How to make an accurate and precise timer in JavaScript

If you want to use a #timer in #JavaScript for something where it's important that it's #accurate - like playing a drum loop - the built in JavaScript timer functions are virtually unusable.

In this tutorial we will have a look at how inaccurate the built in JavaScript timer functions are. We will hear how horrible it sounds when using them to play a drum loop. Then we will explore how we can account for the drift and create our own accurate timer. AND we will create our very own timer constructor function, so we can have multiple accurate and precise timer instances running simultaneously. We will also talk about why that is not always a good idea, and then - as a bonus - we will create a cute little drum machine. https://www.youtube.com/watch?v=x8PBWobv6NY

Top comments (0)