Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aeeed99/goodtimer
A setTimeout/setInterval for Humans (and so much more)
https://github.com/aeeed99/goodtimer
countdown milliseconds npm npm-package time timer
Last synced: 2 months ago
JSON representation
A setTimeout/setInterval for Humans (and so much more)
- Host: GitHub
- URL: https://github.com/aeeed99/goodtimer
- Owner: aeeed99
- License: mit
- Created: 2016-07-31T16:36:54.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2024-07-27T14:12:40.000Z (6 months ago)
- Last Synced: 2024-11-01T03:35:31.960Z (2 months ago)
- Topics: countdown, milliseconds, npm, npm-package, time, timer
- Language: TypeScript
- Homepage: https://aeeed99.github.io/goodtimer
- Size: 7.45 MB
- Stars: 50
- Watchers: 3
- Forks: 1
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-github-repos - aeeed99/goodtimer - A setTimeout/setInterval for Humans (and so much more) (TypeScript)
README
The Split-Second Precise JavaScript Timer
---
![goodtimer demo](assets/example1.gif)
## 🧐 About
Goodtimer provides an accurate-to-milliseconds way of implementing `setTimeout` and `setInterval`. It's the timer of your
dreams, providing a high-level API to easily manipulate countdowns. Here's a few things that make Goodtimer so good:* It self-corrects delays from the event loop, so it's guaranteed to stay in sync with time.
* It comes with a flexible [`timeExpression`](#timeexpressions) syntax, so you can easily express time in a number of desirable ways.
* Provides [drop-in replacement](docs/timeutil.md) to `setInterval`.
* Can be used in projects like react with npm, or directly in the browser via [cdn](https://cdn.nickpal.to/goodtimer);## Installation & simple usage
Download using [npm](https://npmjs.org/package/goodtimer)
```shell
npm i --save goodtimer
```And use in your code!
```javascript
const { Timer } = require('goodtimer');new Timer('1:00');
```Or replace your drifty `setInterval`s with `setGoodInterval` ⭐️:
```javascript
const { setGoodInterval } = require('goodtimer').timeutil;setGoodInterval(() => console.log("exactly 1 second!"), 1000);
```#### 💝 _Browser-compatible client-side version now available!_
```html
new goodtimer.Timer('1:00');
```
➡️ Jump into the full docs site [here](https://goodtimer.dev) or read below for a few more quick examples :bow:
---
## ⏲ Simple Usage
```javascript
const yourFn = () => {};
new Timer('1:00', yourFn); // replacement for setTimeout
new Timer('1:00', yourFn, { repeat: true }); // replacement for setIntervalconst timer = new Timer('5:00'); // (Five minutes)
timer.pause(); // freezes timer at given time
timer.unpause(); // resumes timer
timer.reset(); // resests to initial value (in this case 5 minutes)
timer.toString() // returns in UTC-like format ("5:00.000")
// ~ 1 second later ~
timer.fmtTime("%M minutes %s seconds") // -> "4 minutes 59 seconds" (many ways to use!)
timer.gt('1:00'); // "greater than" -> true
timer.lt('60:00:00'); // "less than (60 hrs)" -> true
timer.equals('6m'); // (6 minutes, alternate notation) -> false// or use the Time class and skip the controls
const [minute, second] = [new Time('1m'), new Time('1s')];minute.gt(second) // -> true
second.equals(':01') // -> true
minute.equals(second) // -> false
second.set(minute) // set to new value
minute.equals(second) // -> true
minute.toString() // -> "1:00.000"// `timeExpressions` are passed to Time or Timer, and can be an
// object, number, array, or string (in multiple formats)
// below are all the ways to write "25 minutes and 500 milliseconds"new Time('25:00.5'); // string in UTC-like syntax
new Time('25m500ms'); // string with unit annotation
new Time(1500500); // number for milliseconds
new Time({ // object with full names
minutes: 25,
milliseconds: 500
});
```Ready to jump in? See the [full Documentation site](https://goodtimer.dev) spec for many more uses and tutorials!
## :clap: Supporters
[![Stargazers repo roster for goodtimer](https://reporoster.com/stars/aeeed99/goodtimer)](https://github.com/aeeed99/goodtimer/stargazers)
[![Forkers repo roster for goodtimer](https://reporoster.com/forks/aeeed99/goodtimer)](https://github.com/aeeed99/goodtimer/network/members)---