This simple setTimeout wrapper is written in TypeScript and works both for browser and Node.
npm i @excalibur-enterprise/timeval-jsTimeout 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(): voidExample:
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(): voidExample:
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(): voidExample:
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();