Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pilaton/sleep-sleep
A tiny and simple JavaScript code execution delay library.
https://github.com/pilaton/sleep-sleep
delay javascript playwright puppeteer sleep sleep-timer typescript
Last synced: about 1 month ago
JSON representation
A tiny and simple JavaScript code execution delay library.
- Host: GitHub
- URL: https://github.com/pilaton/sleep-sleep
- Owner: Pilaton
- License: mit
- Created: 2023-11-22T18:16:00.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-06T12:44:26.000Z (8 months ago)
- Last Synced: 2024-05-07T12:52:42.162Z (8 months ago)
- Topics: delay, javascript, playwright, puppeteer, sleep, sleep-timer, typescript
- Language: TypeScript
- Homepage: https://npmjs.com/package/sleep-sleep
- Size: 147 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# sleep-sleep 😴
A tiny and simple JavaScript code execution delay library.
**0.35 kB!** 🚀 and **No dependencies** 🎉Under the hood, setTimeout is used, so it's simple and reliable.
## Table of contents
- [Install](#install)
- [Usage](#usage)
- [API](#api)## Install
Step 1:
```bash
npm install sleep-sleep
```Step 2:
```js
import sleep from 'sleep-sleep';
```Or if you want to use CommonJS:
```js
const sleep = require('sleep-sleep');
```## Usage
An example of using the sleep function:
```js
import sleep from 'sleep-sleep';async function demoFunction() {
console.log('3-second delay...');
await sleep(3000); // 3000 ms delay (3 seconds)
console.log('Delay completed.');
}demoFunction();
```You can also use a random delay between the minimum and maximum time value:
```js
import sleep from 'sleep-sleep';async function demoFunction() {
console.log('Random delay...');
await sleep(1000, 5000); // Random delay between 1 and 5 seconds
console.log('Delay completed after e.g. 3.47 seconds.');
}demoFunction();
```Random delay is very useful for example in [Puppeteer](https://pptr.dev/) and [Playwright](https://playwright.dev/) related projects, where a random pause between actions gives your automation a more human-like 👨🌾 appearance.
## API
```js
sleep(minMs: number, maxMs?: number): Promise
````minMs`: Minimum number of milliseconds for delay.
`maxMs` (optional): The maximum number of milliseconds for the delay. If not specified, the delay will be fixed and equal to `minMs`