How to change the delay time in a timer while the timer is running - AS3
Copy link to clipboard
Copied
Hello
I want to be able to change the delay time of my timer event with an Input Text box while the timer event is firing.
I managed to code the timer but it will only take the delay before the timer event is started, it then ignores any new delays entered into the Input Text box once the timer is started.
I have tried to move the timerDelay variable into the function just before simTimer.start(); but then the timer is giving an error with a null value for the delay.. i guess its because the timer is outside the function..
Can you please help me to get the delay time to change the timer dynamically when a value is entered into the Input Text box (timerDelayInputBox) while the timer is firing?
//-- My Code
startBtn.addEventListener(MouseEvent.CLICK, runTimer);
stopBtn.addEventListener(MouseEvent.CLICK, stopTimer);
resetBtn.addEventListener(MouseEvent.CLICK, resetTimer);
var timerDelay: Number = Number(timerDelayInputBox.text)
var simTimer: Timer = new Timer(1000*(1/timerDelay));
function runTimer(e:MouseEvent):void {
simTimer.start();
}
//--end of the piece of code I am struggling with...
THANKS
Copy link to clipboard
Copied
Not tested, but something like this should work. You just have to stop the old timer and create a new one.
var simTimer:Timer;
function runTimer(e:MouseEvent):void {
if (simTimer) {
simTimer.stop();
// (remove any simTimer event listener here)
}
var timerDelay:Number = Number(timerDelayInputBox.text);
if (timerDelay) {
simTimer = new Timer(1000 * (1 / timerDelay));
// (add your simTimer event listener here)
simTimer.start();
}
}
Copy link to clipboard
Copied
I copied the code and pasted it into my code but it did not solve my case, BUT your comment helped me to understand the timer better... so I coded the timer inside a button function, so a person can change the delay input box and when the button is clicked the timer will update according to the new delay.. this is working.
thanks for helping me - I appreciate it

