Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Kai001
Posts: 200
Registered: ‎03-10-2011
Accepted Solution

Event when time changes?

[ Edited ]

I want to be able to run a function at an exact time specified by the user. The time will always be an exact hour and minute, so I would think I only need to check once a minute. But, I want it to be accurate to happening at the point where the minute changes. For example, if a user sets the time to "4:30" I want the function to run as soon as the clock on the PlayBook changes to 4:30 (preferably as close as possible). But if my timer runs a listening function every minute and it starts in between minutes for example "2:00:30" then it won't know it's "2:01" until it's "2:01:30", exactly one minute after it started.

 

I could run a function on every frame, but I didn't think that that would be the best way of doing it.

EDIT: I guess the obvious solution to some could be to just check once a second, but is this really the most efficient way?

 

Is there some sort of time event listener, or some other way of doing this?

Please use plain text.
Developer
peter9477
Posts: 6,445
Registered: ‎12-08-2010
My Carrier: none

Re: Event when time changes?

I can think of ways of doing what you want effectively, except in one case you don't cover. 

 

What do you want to happen when the user changes the system time on you?

 

Also, how important is it really to be exactly "on time" (given the rather fluid nature of wall-clock time)?  Maybe your requirement is overly strict.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
Trusted Contributor
PBDev
Posts: 108
Registered: ‎12-28-2010
My Carrier: Verizon

Re: Event when time changes?

This is untested but it might do what you want:

 

import flash.events.TimerEvent;
import flash.utils.Timer;

...

var secondsTimer:Timer = new Timer(1000);	// 1000 milliseconds or a second delay
secondsTimer.addEventListener(TimerEvent.TIMER, onSecondsTimer);
secondsTimer.start();	// start timer

...

secondsTimer.stop();	// stop timer


private function onSecondsTimer(e:TimerEvent):void
{
	var currentTime:Date = new Date();	// get current date/time

	if(currentTime.getHours() == userSetHour && currentTime.getMinutes() == userSetMinute)
	{
		do something
	}

}

 

Please use plain text.
Developer
Kai001
Posts: 200
Registered: ‎03-10-2011

Re: Event when time changes?

[ Edited ]

Maybe I am being a little to strict about it. :smileytongue:

 

But I think, PBDev that your solution is probably the best right now! Thank you very much!!

Please use plain text.
Regular Contributor
macwarrior
Posts: 75
Registered: ‎03-21-2011
My Carrier: T-Mobile

Re: Event when time changes?

Mike's solution will work.

 

One thing to keep in mind, that I had to learn the hard way, is that you can't do math with getHours() and getSeconds() and such, because they roll over at 12 and 60 and so on. If you try to, say, subtract 168 hours from the Hours value to go back in time one week, the code will break badly. Instead, use getTime() which returns an absolute value (UNIX time I think?) in milliseconds and then do the math on that.

Please use plain text.
Developer
Kai001
Posts: 200
Registered: ‎03-10-2011

Re: Event when time changes?

Oh ok, thanks for that, knowing this could save me a lot of time!

Please use plain text.