https://github.com/lambdacasserole/shell-sleeper
An alternative to setTimeout and setInterval powered by the shell sleep command.
https://github.com/lambdacasserole/shell-sleeper
javascript npm setinterval settimeout shell typescript
Last synced: 11 months ago
JSON representation
An alternative to setTimeout and setInterval powered by the shell sleep command.
- Host: GitHub
- URL: https://github.com/lambdacasserole/shell-sleeper
- Owner: lambdacasserole
- License: mit
- Created: 2022-10-26T06:47:19.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-26T13:06:16.000Z (over 3 years ago)
- Last Synced: 2025-03-14T07:17:49.616Z (12 months ago)
- Topics: javascript, npm, setinterval, settimeout, shell, typescript
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# shell-sleeper
An alternative to setTimeout and setInterval powered by the shell sleep command.
## Requirements
- Node.js 14 or newer
- A system-level `sleep` utility (comes with most Linux distros) to get the most from the library
## Installation
Exhaustion can be installed using `npm`:
```sh
npm install --save shell-sleeper
```
## Usage
Usage is very straightforward, `shell-sleeper` can serve as a drop-in replacement for `setInterval` and `setTimeout`:
```js
import {
isSleepAvailable,
setShellInterval,
setShellTimeout,
clearShellTimeout,
clearShellInterval,
shellSleep,
} from 'shell-sleeper';
// Check if a system-level shell `sleep` utility is available.
if (isSleepAvailable()) {
console.log('System-level sleep is available!');
} else {
console.log('System-level sleep is not available!');
}
// setShellInterval and setShellTimeout function identically to setInterval and setTimeout.
const interval = setShellInterval(() => console.log("Will execute every 5 seconds."), 5000);
const timeout = setShellTimeout(() => console.log("Will execute after 5 seconds."), 5000);
// You can clear them in just the same way too.
clearShellInterval(interval);
clearShellTimeout(timeout);
// An extra `shellSleep` command is also included.
(async () => {
await shellSleep(10000); // Will block for 10 seconds.
})();
```
If a system-level shell-based `sleep` utility it not available, the library will fall back to use `setInterval` and `setTimeout`.
## Related Projects
This library uses the [command-exists](https://www.npmjs.com/package/command-exists) package to check for the existence of an available `sleep` command-line utility.
## License
[MIT](LICENSE) © [lambdacasserole](https://github.com/lambdacasserole).