You could have a repeating timer with a short interval, 100ms or so, and check new Date().getTime()
on each tick. By default, a new Date
has the current system time, and getTime()
gives you the time in milliseconds for a simpler comparison. Compare that with a start time and end the game once at least 30 seconds have passed. Something like:
import flash.utils.Timer;
import flash.events.TimerEvent;
var ticker:Timer = new Timer(100, 0);
ticker.addEventListener(TimerEvent.TIMER, checkElapsedTime);
var startTime:Number = new Date().getTime();
ticker.start();
function checkElapsedTime(e:TimerEvent):void {
var now:Number = new Date().getTime();
if (now - startTime >= 30 * 1000) {
// End the game and stop the ticker.
ticker.stop();
ticker.removeEventListener(TimerEvent.TIMER, checkElapsedTime);
}
}
There is obviously still some slight lag if the timer is very slow, so just in case I would run the same check when an answer is submitted and ignore it if it was after the deadline.
Finally, to check if a user has changed their system clock you could store the previous now
and make sure the new now
is always larger.