Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maximilianmairinger/tinydelay
A promise based timing functions implementation. (timeout / interval).
https://github.com/maximilianmairinger/tinydelay
delay interval timeout tiny
Last synced: about 2 months ago
JSON representation
A promise based timing functions implementation. (timeout / interval).
- Host: GitHub
- URL: https://github.com/maximilianmairinger/tinydelay
- Owner: maximilianMairinger
- Created: 2020-03-03T15:40:49.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-12-13T00:32:03.000Z (about 1 year ago)
- Last Synced: 2024-11-11T01:46:29.874Z (2 months ago)
- Topics: delay, interval, timeout, tiny
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/tiny-delay
- Size: 33.2 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Tiny delay
Changeable and cancelable promise based delay / setTimeout implementation.
## Example
Simple promise / callback based usage
```ts
import delay from "tiny-delay"delay(1000).then(() => {
console.log("1 second passed")
})delay(1000, () => {
console.log("1 second passed")
})
```Cancel anytime
```ts
const timeout = tinyDelay(1000)delay(500, () => {
timeout.cancel()
})
```Change timeout duration. For doc on Data see: https://github.com/maximilianMairinger/josm#data
```ts
import { Data } from "josm"const timeoutDuration = new Data(1000)
const timeout = delay(timeoutDuration, () => {
console.log("2 seconds passed")
})delay(500, () => {
timeoutDuration.set(2000)
})
```### Additional Functions
#### isIdle
```ts
import { isIdle } from "tiny-delay"const msWhenToCallItIdle = 500
const { idle, f: notIdle } = isIdle(msWhenToCallItIdle)element.on("scroll", notIdle)
// idle is a Data that is true when the element has not been scrolled for msWhenToCallItIdle milliseconds. See https://github.com/maximilianMairinger/josm#data
idle.get((idle) => {
console.log(idle)
})
```#### absoluteToDeltaTime
```ts
import { absoluteToDeltaTime } from "tiny-delay"const absoluteTime = Date.now() + 1000
const deltaTimeRightNow = absoluteToDeltaTime(absoluteTime) // = 1000// may be chained to delay
delay(deltaTimeRightNow, () => {
console.log("1 second passed")
})// A Data instance can also be given.
```#### decomposedAbsoluteToDeltaTime
Basically just a wrapper over absoluteToDeltaTime, but decomposed into duration and start time. Both can be instance of Data.
A potential use case would be a login session that has a starting time and a duration, which can be reconstructed when the server restarts.
```ts
import { decomposedAbsoluteToDeltaTime } from "tiny-delay"const durationTime = 1000
const startingTime = Date.now() - 500
const deltaTimeRightNow = decomposedAbsoluteToDeltaTime(absoluteTime, startingTime)// may be chained to delay
delay(deltaTimeRightNow, () => {
console.log(".5 second passed from now, 1 second from .5 seconds ago")
})
```## Contribute
All feedback is appreciated. Create a pull request or write an issue.