# true-timeout

> True Timeout is a Web Worker based utility that gives you the ability to have a timeout that is closest to a True Timeout unlike setTimeout (provided by the browser) that is based on the event loop.

Latest version **1.0.0** (published 2018-06-08) · 0 weekly downloads

## Install

```sh
npm install true-timeout
pnpm add true-timeout
yarn add true-timeout
bun add true-timeout
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-06-08 |
| First published | 2018-03-17 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 77 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | revanth0212 |

## Links

- npm: https://www.npmjs.com/package/true-timeout
- Repository: https://github.com/revanth0212/true-timeout
- Issues: https://github.com/revanth0212/true-timeout/issues
- npm.io page: https://npm.io/package/true-timeout

## Recent versions

- 1.0.0 (latest) — 2018-06-08
- 0.1.0 — 2018-03-17

## README

# True Timeout

True Timeout is a Web Worker based utility that gives you the ability to have a timeout that is closest to a True Timeout unlike setTimeout (provided by the browser) that is based on the event loop.

An example can be seen here: https://revanth0212.github.io/true-timeout/

To start using it in your projects,

    npm install true-timeout

# Motivation

In languages like Java it is possible to have a timeout that is closer to a True Timeout, but when it comes to JavaScript that works on a single thread, true timeout is never achieved.

Current API that does timeout in JavaScript is **setTimeout** which is provided by the broswer. When you call setTimeout with a callback function and a timeout mentioned in milliseconds, the browser initiates a browser thread that will run the timeout logic and once done, puts the callback function in the JavaScript **event loop**. 

Since the JavaScript engine is single threaded it can only run functions from a **single stack**. To implement timeout kind of Async operations, JavaScript engine picks up operations form event loop when ever it had nothing to do meaning the stack is empty.

**So now setTimeout is dependent on event loop and hence it can only guarantee minimum timeout and not exact timeout.**

For instance:

If we run the following in browser, a browser thread will be spawned and it will wait till 3 seconds after which it will put the alert call into the event loop.

    setTimeout(function(){ alert("Hello"); }, 3000);

If the event loop already has some events pending, the alert call will stay there till the stack is empty and till all the pending events are addressed.

# Solution: trueTimeout

 Solution to this is a Worker based solution. True timeout works on a web worker which is also a thread by itself but it does not depend on the event loop.

# Usage

    window.trueTimeout(trueTimeoutCallBack, timeout, failureHandler)

**trueTimeout** is injected into **global scope** so you can start using it just like you would use setTimeout. 

**failureHandler is optional.**

# Params

 1. callback function which will be called once the timeout expires. **Required**. `() => void`
 2. timeout in milliseconds. **Required**. `number`
 3. errorHandler funciton which will be called if there is any error encountered in the whole process. **Optional**. `() => void`

# Sample

Check out [index.html](https://github.com/revanth0212/true-timeout/blob/master/index.html)

# # Wanna Contribute?

Please fork/branch the project in github and once done create a PR.

To run the project locally for development make sure you have [http-server](https://www.npmjs.com/package/http-server) package installed globally.

    npm run start

---
_Source: https://npm.io/package/true-timeout · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
