This simple setTimeout
wrapper is written in TypeScript and works both for browser and Node.
npm i @excalibur-enterprise/timeval-js
Timeout class instantiation is made by constructor with params.
Prototype:
constructor( private callback: () => void, private ms: number );
Params:
callback
: Callback invoked once timeout expirems
: Time of expiry in milliseconds
Example:
import { Timeout } from '@excalibur-enterprise/timeval-js';
const timeout = new Timeout( () =>
{
console.log( 'Timeout expired!' );
}, 5000 );
Clears timeout.
Prototype:
public clear(): void
Example:
import { Timeout } from '@excalibur-enterprise/timeval-js';
const timeout = new Timeout( () =>
{
// This never gets called
console.log( 'Timeout expired!' );
}, 5000 );
new Timeout( () =>
{
// Clears first timeout before it expires
timeout.clear();
}, 3000 );
Refreshes timeout as it is newly created with same expiry time.
Prototype:
public refresh(): void
Example:
import { Timeout } from '@excalibur-enterprise/timeval-js';
const timeout = new Timeout( () =>
{
console.log( 'Timeout expired!' );
}, 5000 );
// Refreshes previous timeout object after 3 seconds, so it gets expired after 8 seconds
new Timeout( () =>
{
timeout.refresh();
}, 3000 );
When called, the active Timeout object will not require the Node.js event loop to remain active.
Has effect only when run under Node
Prototype:
public unref(): void
Example:
import { Timeout } from '@excalibur-enterprise/timeval-js';
const timeout = new Timeout( () =>
{
console.log( 'Timeout expired!' );
}, 5000 );
// Do not require the Node.js event loop to remain active
timeout.unref();