npm package: @iqrok/intime
Description: Interval and Timeout in microseconds
Author: iqrok
Latest version: 0.1.1

# InTime

SetTimeout & SetInterval in microseconds.

This package is using Node-API and the implentation is done in C++, so it only works in Node.js.

The timing is implemented by using sleep. In Linux, [clock_nanosleep](https://man7.org/linux/man-pages/man2/clock_nanosleep.2.html) is used to invoke sleep. In Windows, [SetWaitableTimer](https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-setwaitabletimer) is used instead. If more accurate timing is needed, then busy wait can be used by setting busy wait period. If busy wait period is longer than sleep period, then busy wait will be disabled.

## Installation
```bash
npm i @iqrok/intime
```

## Usage

```javascript
const intime = require('@iqrok/intime');
const { SetInterval, SetTimeout, ClearInterval, CancelTimeout } = intime;

// SetInterval 25,000 us
const interval1 = SetInterval(elapsed_us => console.log(elapsed_us), 25_000);

// SetInterval 30,000 us with 200 us busy wait
const interval2 = SetInterval(elapsed_us => console.log(elapsed_us), 30_000, 200);

// clear interval1 after 1 second
SetTimeout(elapsed_us => {
	ClearInterval(interval1);
	console.log('Done in', elapsed_us / 1e6, 's');
}, 1000_000);

// clear interval2 after 2 seconds
SetTimeout(elapsed_us => {
	ClearInterval(interval2);
	console.log('Done in', elapsed_us / 1e6, 's');
}, 2000_000);

const canceled = SetTimeout(elapsed_us => {
	console.log('this callback won\'t be called');
}, 1500_000);

CancelTimeout(canceled);
```

## Methods

- __SetInterval(callback, us[, busy])__
	|   params   |    Type    |        |        Description               |
	|------------|------------|--------|----------------------------------|
	|`callback`  |__Function__|        |Callback function to be executed  |
	|`us`        |__Number__  |        |delay in microseconds             |
	|`busy`      |__Number__  |optional|busy wait duration in microseconds|
	|                                                                     |
	|`Returns`   |__Number__  |        |SetInterval id                    |


- __ClearInterval(id)__
	|   params   |    Type    |        |        Description               |
	|------------|------------|--------|----------------------------------|
	|`id`        |__Number__  |        |SetInterval id to be cleared      |
	|                                                                     |
	|`Returns`   |__Promise__ |        |Rejects if id is not found        |

- __SetTimeout(callback, us[, busy])__
	|   params   |    Type    |        |        Description               |
	|------------|------------|--------|----------------------------------|
	|`callback`  |__Function__|        |Callback function to be executed  |
	|`us`        |__Number__  |        |delay in microseconds             |
	|`busy`      |__Number__  |optional|busy wait duration in microseconds|
	|                                                                     |
	|`Returns`   |__Number__  |        |SetTimeout id                     |


- __CancelTimeout(id)__
	|   params   |    Type    |        |        Description               |
	|------------|------------|--------|----------------------------------|
	|`id`        |__Number__  |        |SetTimeout id to be canceled      |
	|                                                                     |
	|`Returns`   |__Promise__ |        |Rejects if id is not found        |

	___Note___: Canceling timeout won't clear the timeout itself, it only makes the callback not to be executed once the timeout is done.

- __DelayMs(ms[, busy_ms])__
	|   params   |    Type    |        |        Description               |
	|------------|------------|--------|----------------------------------|
	|`ms`        |__Number__  |        |Delay in milliseconds             |
	|`busy_ms`   |__Number__  |optional|Busy Wait duration in milliseconds|

- __DelayUs(us[, busy_us])__
	|   params   |    Type    |        |        Description               |
	|------------|------------|--------|----------------------------------|
	|`us`        |__Number__  |        |Delay in microseconds             |
	|`busy_us`   |__Number__  |optional|Busy Wait duration in microseconds|

### Callback
|   params   |    Type    |        |        Description                                  |
|------------|------------|--------|-----------------------------------------------------|
|`elapsed_us`|__Number__  |        |Duration of SetInterval or SetTimeout in microseconds|
___Note___: Currently, the callback can't accept additional parameters.