Skip to main content
December 12, 2015
Answered

How to create pause function in timer?

  • December 12, 2015
  • 1 reply
  • 1386 views

Hi. I want to add timer function inside my project which when user pause the timer will pause and resume it back when user press play. Anyone know how to make it work since I have tried few coding it didnt work.

Here is my code.

var endTime:int = getTimer();

endTime += 40*60*1000;  //adjust endTime to 15 minutes in the future.

var countdownTimer:Timer = new Timer(1000);

countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);

countdownTimer.start();

function updateTime(e:TimerEvent):void

{

  var timeLeft:Number = endTime - getTimer();

  var seconds:Number = Math.floor(timeLeft / 1000);

  var minutes:Number = Math.floor(seconds / 60);

  seconds %= 60;

  minutes %= 60;

  var sec:String = seconds.toString();

  var min:String = minutes.toString();

  if (sec.length < 2) {

    sec = "0" + sec;

  }

  if (min.length < 2) {

    min = "0" + min;

  }

  var time:String = min + ":" + sec;

  countdown.text = time;

}

This topic has been closed for replies.
Correct answer nezarov

Using "getTimer()" on timer update will not allow you to pause the timer properly, so you need to count the remaining time in another way:

var endTime:int = getTimer();

endTime += 40*60*1000;  //adjust endTime to 15 minutes in the future.

var countdownTimer:Timer = new Timer(1000);

countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);

countdownTimer.start();

function updateTime(e:TimerEvent):void

{

    endTime -= 1000

    var timeLeft:Number = endTime;

  var seconds:Number = Math.floor(timeLeft / 1000);

  var minutes:Number = Math.floor(seconds / 60);

  seconds %= 60;

  minutes %= 60;

  var sec:String = seconds.toString();

  var min:String = minutes.toString();

  if (sec.length < 2) {

    sec = "0" + sec;

  }

  if (min.length < 2) {

    min = "0" + min;

  }

  var time:String = min + ":" + sec;

  countdown.text = time;

}

// this button will pause the timer using Boolean variable:

var pause:Boolean = false;

my_btn.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent):void

{

    if(pause == false)

    {

        countdownTimer.stop();

        pause = true;

    }

    else

    {

        countdownTimer.start();

        pause = false;

    }

}

1 reply

nezarovCorrect answer
Inspiring
December 12, 2015

Using "getTimer()" on timer update will not allow you to pause the timer properly, so you need to count the remaining time in another way:

var endTime:int = getTimer();

endTime += 40*60*1000;  //adjust endTime to 15 minutes in the future.

var countdownTimer:Timer = new Timer(1000);

countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);

countdownTimer.start();

function updateTime(e:TimerEvent):void

{

    endTime -= 1000

    var timeLeft:Number = endTime;

  var seconds:Number = Math.floor(timeLeft / 1000);

  var minutes:Number = Math.floor(seconds / 60);

  seconds %= 60;

  minutes %= 60;

  var sec:String = seconds.toString();

  var min:String = minutes.toString();

  if (sec.length < 2) {

    sec = "0" + sec;

  }

  if (min.length < 2) {

    min = "0" + min;

  }

  var time:String = min + ":" + sec;

  countdown.text = time;

}

// this button will pause the timer using Boolean variable:

var pause:Boolean = false;

my_btn.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent):void

{

    if(pause == false)

    {

        countdownTimer.stop();

        pause = true;

    }

    else

    {

        countdownTimer.start();

        pause = false;

    }

}

December 12, 2015

Thank you nezarov! You reallly help me May God Bless You

However, can you explain to me why getTimer() is impossible to pause timer? Ive seen this few times but still, I dont understand why.

Inspiring
December 12, 2015

You're welcome and thank you too

The "getTimer()" method returns the number of milliseconds that have elapsed since the runtime started not the timer, as you cannot control or stop it.

Best regards.