0.1.1 • Published 8 months ago

@iqrok/intime v0.1.1

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
8 months ago

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 is used to invoke sleep. In Windows, 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

npm i @iqrok/intime

Usage

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

paramsTypeDescription
elapsed_usNumberDuration of SetInterval or SetTimeout in microseconds

Note: Currently, the callback can't accept additional parameters.

0.1.1

8 months ago

0.1.0

8 months ago