Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Timer

Counts the duration of runtime operations, with pausing capabilities.

An instance of Timer is able to count the duration between its start and stop, as well as pausing, which temporarily stops the time counting. Every start-stop cycle resets the object to its default state.

See BasicTimer for a simpler timer implentation (less overhead) and StopWatch for time segmentation.


Examples

  1. Using a single Timer to perform consecutive countings

    import { Timer } from "timecount/utils";
    
    const timer = new Timer();
    
    while (thereAreThingsToDo) {
        timer.start();
    
        doTheThings();
    
        timer.stop().to("second");
        // 0.960041 s
    }
    
  2. Using a TimeWriter to write Timer results

    import { TimeWriter } from "timecount";
    
    const timer = new Timer();
    const timeWriter = new TimeWriter({ verbose: true });
    
    timer.start();
    
    timeWriter.write(timer.elapsedTime, "millisecond");
    // 0.54021 milliseconds
    
    doSomething();
    
    const time = timer.stop();
    
    timeWriter.write(time, "millisecond");
    // 10156.663207 milliseconds
    
    timeWriter.write(time, "second");
    // 10.156663207 seconds
    
    timeWriter.write(time, "minute");
    // 0.169277720116666668 minute
    
  3. Pausing and resuming the timer

    const timer = new Timer(true);
    
    thingsYouWantMeasured(); // Let's say this takes 5 seconds
    
    timer.pause();
    
    thingsYouDontWantMeasured(); // And this another 5 seconds
    
    timer.stop().to("second");
    // 5 s (paused time is not counted)
    

Hierarchy

Index

Constructors

constructor

  • new Timer(autoStart?: boolean): Timer
  • Initializes a new instance of the BasicTimer class, optionally auto-starting it.

    Parameters

    • Default value autoStart: boolean = false

      Determines whether the timer will immediately start; default is false.

    Returns Timer

Properties

Protected Optional _pauseStartTime

_pauseStartTime: Decimal

Protected field with the time value representing the moment the pausing started.

Protected _pauseTimeSum

_pauseTimeSum: Decimal = new Decimal(0)

Protected field with the time value representing the sum of all previous pause times.

Protected Optional _startTime

_startTime: Decimal

Protected field with the time value representing the moment the timer started.

Accessors

elapsedPauseTime

  • get elapsedPauseTime(): Time

elapsedTime

  • get elapsedTime(): Time

isPaused

  • get isPaused(): boolean

isRunning

  • get isRunning(): boolean

Protected timerErrorType

  • get timerErrorType(): string

totalPauseTime

  • get totalPauseTime(): Time
  • Gets the total amount of time the object spent paused during the current start-stop cycle.

    Returns Time

Methods

Protected getElapsedTime

  • getElapsedTime(): Time

Protected getIsRunning

  • getIsRunning(): boolean

pause

  • pause(): Decimal
  • Prevents the object from counting the time until it is resumed.

    Both the elapsed time and the time resulting from stop won't change during the pause, but pausedTime and totalPauseTime will.

    throws

    TimerError When the timer has not yet started -or- it is already paused.

    Returns Decimal

resume

  • Resumes the time counting, recovering the object from a paused state.

    throws

    TimerError When the timer has not yet started -or- it is not paused.

    Returns Time

    The amount of time the timer spent paused.

start

  • start(): Decimal
  • Begins to count time, setting the start time to a value equivalent to now.

    throws

    TimerError When the timer is already running.

    Returns Decimal

    Time that represents now (or better yet, the moment the timer has started).

stop

  • Ends the time counting, returning the total elapsed time and resetting the object to its default state.

    If the timer is paused, there is no need to resume before calling this method.

    throws

    TimerError When the timer has not yet started.

    Returns Time

    A time with the total amount of nanoseconds spent between start and now.

Generated using TypeDoc