Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BasicTimer

Counts the duration of runtime operations.

An instance of BasicTimer is able to count the duration between its start and stop. Every start-stop cycle resets the object to its default state.

See Timer if you need pausing and StopWatch for time segmentation (laps).


Examples

  1. Using a single BasicTimer to perform consecutive countings

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

    import { TimeWriter } from "timecount";
    import { BasicTimer } from "timecount/utils";
    
    const timer = new BasicTimer();
    const writer = 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
    

Hierarchy

Index

Constructors

constructor

  • 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 BasicTimer

Properties

Protected Optional _startTime

_startTime: Decimal

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

Accessors

elapsedTime

  • get elapsedTime(): Time
  • Gets the time elapsed since the start of the timer until now.

    Returns Time

isRunning

  • get isRunning(): boolean

Protected timerErrorType

  • get timerErrorType(): string
  • A protected property useful when overriding the timers it changes the output of timer errors in order to represent the specified name.

    Returns string

Methods

Protected getElapsedTime

  • getElapsedTime(): Time

Protected getIsRunning

  • getIsRunning(): boolean

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.

    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