New:Introducing Socket Scanning for VS Code Marketplace Extensions.Learn more →
Get Started

@gibme/timer

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@gibme/timer

A simple timer/metronome

latest
Source
npmnpm
Version
22.0.0
Version published
Weekly downloads
66
247.37%
Maintainers
1
Weekly downloads
 
Created
Source

Event-based Timer/Metronome

A simple, event-driven timer library for Node.js built on EventEmitter. Provides a base Timer for interval-based ticks, plus SyncTimer and AsyncTimer helpers that execute functions on each tick and emit typed results.

Documentation

https://gibme-npm.github.io/timer/

Installation

npm install @gibme/timer

or

yarn add @gibme/timer

Usage

Basic Timer

import Timer from '@gibme/timer';

const timer = new Timer(60_000);

timer.on('tick', () => {
    // runs every 60 seconds
});

timer.start();

Auto-Start with Arguments

const timer = new Timer(5_000, true, 'hello', 42);

timer.on('tick', (greeting: string, value: number) => {
    console.log(greeting, value); // "hello" 42
});

SyncTimer

Wraps a synchronous function, executing it on each interval and emitting the result via the data event.

import { SyncTimer } from '@gibme/timer';

const timer = new SyncTimer(() => Math.random(), 1_000, true);

timer.on('data', (value, timestamp, interval) => {
    console.log(`Got ${value} at ${timestamp}s (every ${interval}ms)`);
});

AsyncTimer

Wraps an asynchronous function, awaiting it on each interval and emitting the resolved value.

import { AsyncTimer } from '@gibme/timer';

const timer = new AsyncTimer(async () => {
    const res = await fetch('https://api.example.com/data');
    return res.json();
}, 30_000, true);

timer.on('data', (payload, timestamp, interval) => {
    console.log(payload);
});

timer.on('error', (error) => {
    console.error('Request failed:', error);
});

sleep

A simple async sleep utility.

import { sleep } from '@gibme/timer';

await sleep(2_000); // wait 2 seconds

API

Timer

new Timer(interval: number, autoStart?: boolean, ...args: any[])
ParameterTypeDefaultDescription
intervalnumberMilliseconds between ticks
autoStartbooleanfalseStart immediately on construction
...argsany[]Arguments forwarded to every tick event

Properties

PropertyTypeDescription
intervalnumberThe tick interval in milliseconds
pausedbooleanWhether the timer is paused
destroyedbooleanWhether the timer has been destroyed

Methods

MethodReturnsDescription
start()voidStart the timer
stop()voidStop the timer
toggle()booleanToggle on/off; returns true if now running
tick(...args)voidForce an immediate tick with optional arguments
destroy()voidPermanently stop and clean up the timer

Events

EventListener SignatureDescription
tick(...args: any[]) => voidEmitted on each interval tick
start() => voidEmitted when the timer starts
stop() => voidEmitted when the timer stops
error(error: Error) => voidEmitted on errors

SyncTimer<Type>

Extends Timer. Executes a synchronous function on each tick.

new SyncTimer(func: () => Type, interval: number, autoStart?: boolean)

Emits a data event after each execution:

timer.on('data', (result: Type, timestamp: number, interval: number) => void)

AsyncTimer<Type>

Extends Timer. Executes an asynchronous function on each tick.

new AsyncTimer(func: () => Promise<Type>, interval: number, autoStart?: boolean)

Emits a data event after each execution resolves:

timer.on('data', (result: Type, timestamp: number, interval: number) => void)

Errors thrown or rejected by the function are emitted as error events.

License

MIT

FAQs

Package last updated on 03 Apr 2026

Related posts