Timer (library)

Categories:Utilities

Overview

Summary

The Timer class is a very simple class designed to measure the time between two events.

Contents

Usage

It has only a few methods and properties, which keeps things simple.

Additionally, tasks can be timed using static methods directly on the Timer class itself, saving the step of instantiating a new instance of a Timer object:

Timer.start();
Timer.stop(true);

API

Timer(name)

Timer constructor

Parameters:

  • name String An optional name for the new Timer

Create a new Timer instance:

var timer = new Timer();

start()

Start the timer

start() can be called on a timer instance, or statically on the Timer class itself.

An instance example:

var timer = new Timer('Decombobulate');
timer.start();

// do some tasks

timer.stop(true);
Timer "Decombobulate" took 1.11 seconds

A static example:

Timer.start('Recombobulate');

// do some tasks

Timer.stop(print);
Timer "Recombobulate" took 1.25 seconds

stop(print)

Stop the timer

Parameters:

  • print Boolean Optinally print the results of the timer

stop() can be called on a timer instance, or statically on the Timer class itself.

See the examples under start() for more information

toString()

Standard toString method

A standard trace reveals the following:

trace(timer);
[object Timer time="1.12 seconds"]

API - Properties

The following properties are available on Timer class instances:

  • time - the time in English of the last-timed instance, such as 1.25 milliseconds, or 1.45 minutes
  • milliseconds - the number of milliseconds of the last-timed instance
  • startDate - the start Date object of the last-timed instance
  • endDate - the end Date object of the last-timed instance

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>