Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

events-timer

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

events-timer

Tracks time between two actions for an event and records entries.

  • 1.0.3
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

EventsTimer NPM Package

The EventsTimer NPM package is a TypeScript module designed to manage timers for various events. It includes features for starting, stopping, and retrieving statistics about timers associated with specific events.

Installation

To install this package, you can use npm or yarn:

npm install events-timer
# or
yarn add events-timer
Usage

Here's how you can use the EventsTimer class in your TypeScript or JavaScript code:

import EventsTimer from 'events-timer';

// Initialize EventsTimer with an empty eventTimers object
const eventsTimer = new EventsTimer({});

// Start a timer for an event
const timerId = eventsTimer.startTimer('eventName');

// Stop a timer for an event
eventsTimer.stopTimer('eventName', timerId);

// Get the average execution time for an event
const averageTime = eventsTimer.getAverageExecutionTime('eventName');

// Show all timers
const allTimers = eventsTimer.showTimers();
Methods
  • startTimer(eventName: string): string: Starts a new timer for the specified event and returns a unique timer ID.
  • stopTimer(eventName: string, timerId: string): void: Stops the timer with the provided timer ID for the specified event.
  • getAverageExecutionTime(eventName: string): number: Calculates and returns the average execution time (in seconds) for timers associated with a specific event.
  • showTimers(): Record<string, Timer[]>: Returns a record of all timers, organized by event name.
Example

Here's a simple example of how to use the EventsTimer:

import EventsTimer from 'events-timer';

const eventsTimer = new EventsTimer({});

const timerId = eventsTimer.startTimer('exampleEvent');

setTimeout(() => {
  eventsTimer.stopTimer('exampleEvent', timerId);
  const averageTime = eventsTimer.getAverageExecutionTime('exampleEvent');
  console.log(`Average Execution Time: ${averageTime} seconds`);
}, 1000);

Example #2 (external state):

import EventsTimer, { Timer } from 'events-timer';

const externalState: Record<string, Timer[]> = {};
const eventsTimer = new EventsTimer(externalState);

const timerId = eventsTimer.startTimer('exampleEvent');

setTimeout(() => {
  eventsTimer.stopTimer('exampleEvent', timerId);
  const averageTime = eventsTimer.getAverageExecutionTime('exampleEvent');
  console.log(`Average Execution Time: ${averageTime} seconds`);
}, 1000);

Example #3 (React useState):

import React, { useState } from 'react';
import EventsTimer from 'events-timer';

// Create an instance of EventsTimer with an external state object
const externalState = {};
const eventsTimer = new EventsTimer(externalState);

// Create a custom React hook for managing timers
function useEventsTimer() {
  const [timers, setTimers] = useState(externalState);

  const startTimer = (eventName) => {
    const timerId = eventsTimer.startTimer(eventName);
    // Update the state with the new timers data
    setTimers({ ...externalState });
    return timerId;
  };

  const stopTimer = (eventName, timerId) => {
    eventsTimer.stopTimer(eventName, timerId);
    // Update the state with the updated timers data
    setTimers({ ...externalState });
  };

  const getAverageExecutionTime = (eventName) => {
    return eventsTimer.getAverageExecutionTime(eventName);
  };

  return { startTimer, stopTimer, getAverageExecutionTime, timers };
}

// Example usage in a React component
function TimerComponent() {
  const { startTimer, stopTimer, getAverageExecutionTime, timers } = useEventsTimer();

  const handleStartTimer = () => {
    const timerId = startTimer('example-event');
    // ... Perform some task
    setTimeout(() => {
      stopTimer('example-event', timerId);
      const averageExecutionTime = getAverageExecutionTime('example-event');
      console.log(`Average Execution Time: ${averageExecutionTime} seconds`);
    }, 3000);
  };

  return (
    <div>
      <button onClick={handleStartTimer}>Start Timer</button>
      <p>Timers: {JSON.stringify(timers)}</p>
    </div>
  );
}

export default TimerComponent;

You can use any external storage for saving the data for the events and the timers.

Dependencies

This package has no external dependencies.

License

This package is released under the MIT License.

Contribution

Feel free to contribute by opening issues or creating pull requests on GitHub.


This README provides a basic overview of the EventsTimer NPM package. For more detailed information and usage examples, please refer to the package's documentation or explore the codebase.

Keywords

FAQs

Package last updated on 08 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc