Skip to content

Isomorphic timeout / interval wrappers for both Node JS & Browser

License

Notifications You must be signed in to change notification settings

excalibur-enterprise/timeval-js

Repository files navigation

@excalibur-enterprise/timeval-js

GitHub issues

Isomorphic timeout / interval wrappers for both Node JS & Browser

This simple setTimeout wrapper is written in TypeScript and works both for browser and Node.

Table of Contents

Installation

npm i @excalibur-enterprise/timeval-js

Documentation

Instantiation

Timeout class instantiation is made by constructor with params.

Prototype:

constructor( private callback: () => void, private ms: number );

Params:

  • callback: Callback invoked once timeout expire
  • ms: Time of expiry in milliseconds

Example:

import { Timeout } from '@excalibur-enterprise/timeval-js';

const timeout = new Timeout( () =>
{
	console.log( 'Timeout expired!' );
}, 5000 );

clear

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 );

refresh

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 );

unref

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();