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

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

nickpalenchar profile image Nick Palenchar ใƒป2 min read

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!
  }
});

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');

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

Discussion

markdown guide