DEV Community

Cover image for t-minus v2.0 - timers/setTimeout with superpowers (a rewrite of my oldest side-project)
Nick Palenchar
Nick Palenchar

Posted on

t-minus v2.0 - timers/setTimeout with superpowers (a rewrite of my oldest side-project)

Earlier this year I decided to take my oldest side project and rewrite it with what I know now.

t-minus v1.0 was written in 2015 as an easy way to set timers, with some emphasis on use in a browser UI.

Specifically, it was born out of an even larger side project that uses pomodoro timers (eattomatoes.herokuapp.com - unmaintained but still somewhat functional)

It utilized callback functions to control what happens as seconds pass, as well as when the timer hits 0.

var timer = new Timer('1:30', {
  onInterval: function() {
    this.getTimerUI(); // human readable string!
  },
  onTimeout: function() {
    // times up!
    this.restart(); // `this` refers to its own timer + handy methods!
  }
});
Enter fullscreen mode Exit fullscreen mode

It had all the methods you'd expect on a timer.

timer.pause
timer.resume
timer.reset
timer.clear

and it was written as a single vanilla JavaScript file

The 2.0 rewrite

const { Timer } = require('t-minus');
Enter fullscreen mode Exit fullscreen mode

I rewrote the entire module from scratch, taking into account v1's README to preserve the interface. I wanted a better written package that's more optimal, but had the same basic API and usage as its original.

The end result has some new features for use:

  • Support for milliseconds (v1's smallest unit was essentially the second)
  • UI/timer setting units up to years (v1's largest unit was the hour)
  • More accurate pausing (v1's pause would stop at the last full second)
  • More memory efficient (v1 would keep setIntervals when a timer is paused; not in v2)

...and new features for development.

  • Unit test coverage 🙌
  • Written in TypeScript 👨‍💻
  • Overall better documented.

If you need timer functions or want a better way to handle countdowns in the browser (or node). Consider giving t-minus an install 😁

If this project gets even one contribution from someone other than me, I would have exceeded my goals with it. Check out the repo if you feel so inclined

Top comments (0)