Highlighted

Simple Countdown Script behaving strange on iOS [HELP]

Engaged ,
Apr 23, 2015

Copy link to clipboard

Copied

I have a basic countdown script that I've used in the past in old Flash project. For some reason when I run it in an iOS app it behaves strange. I am also looking at the best approach to modify so I can set my countdown variable to accept a value such as  2:15  ( Two minutes & 15 seconds ) rather than just whole numbers.

The weirdness is that if I set it to countdown 2 minutes...  I can see the timer start at 2:00   .. but then it jumps to 1:57 and never shows  1:59  1:58

Here is my script... is there a better way to do this ?

As always .. any help is appreciated.

// I would like to be able to use values such as  2:15

var theTime:int = 2;

var endTime:Number = getTimer();

var countdownTimer:Timer = new Timer(1000);

countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);

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;

  }

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

  time_txt.text = time;

 

  if(minutes == 0 && seconds == 0){

  

   trace("TIME ENDED");

  

  countdownTimer.stop();

  

   }

}

function resetTimer():void{

  endTime = getTimer();

  countdownTimer.reset();

}

function startTimer():void{

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

countdownTimer.start();

}

TOPICS
Development

Views

210

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Simple Countdown Script behaving strange on iOS [HELP]

Engaged ,
Apr 23, 2015

Copy link to clipboard

Copied

I have a basic countdown script that I've used in the past in old Flash project. For some reason when I run it in an iOS app it behaves strange. I am also looking at the best approach to modify so I can set my countdown variable to accept a value such as  2:15  ( Two minutes & 15 seconds ) rather than just whole numbers.

The weirdness is that if I set it to countdown 2 minutes...  I can see the timer start at 2:00   .. but then it jumps to 1:57 and never shows  1:59  1:58

Here is my script... is there a better way to do this ?

As always .. any help is appreciated.

// I would like to be able to use values such as  2:15

var theTime:int = 2;

var endTime:Number = getTimer();

var countdownTimer:Timer = new Timer(1000);

countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);

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;

  }

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

  time_txt.text = time;

 

  if(minutes == 0 && seconds == 0){

  

   trace("TIME ENDED");

  

  countdownTimer.stop();

  

   }

}

function resetTimer():void{

  endTime = getTimer();

  countdownTimer.reset();

}

function startTimer():void{

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

countdownTimer.start();

}

TOPICS
Development

Views

211

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 23, 2015 0
Engaged ,
Apr 23, 2015

Copy link to clipboard

Copied

I'm not sure why it jumps a couple seconds, but the easiest way to let your timer accept times like 2:15 is to make theTime in seconds, not minutes.  I.e. at the top you would have:

var theTime:int = 135; // 2:15 is 2 * 60 * 15 seconds

at the bottom inside startTimer, you would have:

endTime += theTime * 1000;

HTH

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2015 0
Engaged ,
Apr 23, 2015

Copy link to clipboard

Copied

This works .. but same thing.. when timer starts .. it's supposed to start at 2:15 ...  but 135 seconds shows it starting from 2:13

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2015 0
Engaged ,
Apr 23, 2015

Copy link to clipboard

Copied

Something must be using up processor time and blocking UI updates.  No idea without seeing more code, but are you initializing things at the same time?  Is this code running directly on app startup?  What other libraries/frameworks are you using?  If you wait several seconds before starting the timer, does the jump go away?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2015 0
Engaged ,
Apr 28, 2015

Copy link to clipboard

Copied

I'm not running anything really that should cause issues here. I start the timer with a button press. Thats it.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 28, 2015 0