12931 Users read it.

Timer

by Patrick Woldberg (May 18, 2006)

This ActionScript 2 Class allows you to create a timer with a start and stop function.

Properties

Value : Return the time in miliseconds

Methods

Start : Start timer
Stop : Stop timer
Reset : Reset timer

User Rating (2 votes)
0 reviews available

class Timer
{
 private var _start:Number;
 private var _pause:Number;
 
 public function get Value():Number
 {
  if (this._start == 0) return 0;
  if (this._pause > 0) return this._pause - this._start;
  var current:Number = getTimer();
  return current - this._start;
 }
 
 public function Timer()
 {
  this.Reset();
 }
 
 public function Start():Void
 {
  if (this._pause > 0)
  {
   this._start += getTimer() - this._pause;
   this._pause = 0;
  }
  else this._start = getTimer();
 }
 
 public function Stop():Void
 {
  if (this._start > 0 && this._pause == 0) this._pause = getTimer();
 }
 
 public function Reset():Void
 {
  this._start = 0;
  this._pause = 0;
 }
}

Sample usage:

Create 3 movieclips on the stage for the start, stop and reset button, name these movieclips start_mc, stop_mc and reset_mc. Create an textfield and make this dynamic, name this textfield timer_txt. Then add the below ActionScript on the first frame. You now have a stopwatch.

var t:Timer = new Timer();

start_mc.onRelease = function() {t.Start();}
stop_mc.onRelease = function() {t.Stop();}
reset_mc.onRelease = function() {t.Reset();}

setInterval(update, 1);

function update()
{
 timer_txt.text = t.Value;
}

Patrick Woldberg

Patrick WoldbergWorking as a developer creating Dreamweaver extensions and designing/programming for the community sites.

See All Postings From Patrick Woldberg >>

Follow us on twitter Subscribe to our RSS feed
Activate your free membership today | Login | Currency