03-26-2011 05:58 AM - edited 03-26-2011 06:41 AM
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?
Solved! Go to Solution.
03-26-2011 08:04 AM
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.
03-26-2011 06:24 PM
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
}
}
03-26-2011 07:50 PM - edited 03-26-2011 07:51 PM
Maybe I am being a little to strict about it. ![]()
But I think, PBDev that your solution is probably the best right now! Thank you very much!!
03-26-2011 07:55 PM
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.
03-26-2011 07:57 PM
Oh ok, thanks for that, knowing this could save me a lot of time!