https://github.com/sdgluck/tiny-tim
:hourglass_flowing_sand: a tiny timer < 160 bytes
https://github.com/sdgluck/tiny-tim
es6 javascript timer tiny tiny-javascript-library
Last synced: 5 months ago
JSON representation
:hourglass_flowing_sand: a tiny timer < 160 bytes
- Host: GitHub
- URL: https://github.com/sdgluck/tiny-tim
- Owner: sdgluck
- License: mit
- Created: 2017-02-02T11:58:01.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-07T17:09:44.000Z (about 9 years ago)
- Last Synced: 2025-07-27T10:08:31.692Z (7 months ago)
- Topics: es6, javascript, timer, tiny, tiny-javascript-library
- Language: JavaScript
- Homepage:
- Size: 104 KB
- Stars: 23
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Tiny Tim
Meet Tim, a simple, small timer
Made with ❤ at @outlandish
:cookie: Comes in at `156 bytes` minified. Zero dependencies.
:sparkles: Tiny Tim is compiled using [`babili`](https://github.com/babel/babili) for ES6+ and CommonJS environments.
:point_right: Use your preferred bundler and transpiler as required.
:page_facing_up: Read the [annotated source code](https://github.com/sdgluck/tiny-tim/blob/master/ANNOTATED.md).
## Install
```sh
npm install --save tiny-tim
```
```sh
yarn add tiny-tim
```
## Import
```js
// ES2015
import timer from 'tiny-tim'
```
```js
// CommonJS
var timer = require('tiny-tim')
```
## Usage
### `timer([unit[, suffix]]) : Function`
Create a timer and start counting!
- __unit__ {String} Unit of time to return _(optional, default=`"s"`)_
- __suffix__ {Boolean} Attach suffix, e.g. unit "s" appends `"s"` _(optional, default=`false`)_
Available units: `"ms"`, `"s"`, `"m"`, `"h"`
Returns a function that stops the timer and returns the duration:
- as a string if with suffix
- or as a number without
## Examples
```js
// Time in seconds with suffix
const seconds = timer('s', true)
setTimeout(() => {
console.log(seconds()) //=> '10s'
}, 10000)
// reuse a timer...
setTimeout(() => {
console.log(seconds()) //=> '15s'
}, 15000)
```
```js
// Time milliseconds without suffix
const milliseconds = timer()
setTimeout(() => {
console.log(milliseconds()) //=> 1000
}, 1000)
```
## Contributing
All pull requests and issues welcome!
If you're not sure how, check out the [great video tutorials on egghead.io](http://bit.ly/2aVzthz)!