Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Random delay for a Timer, in AS3

Guest
Nov 03, 2015 Nov 03, 2015

Hello, i would like to make a timer with a random delay in AS3.

For example, i click on a button and it trace "hello" in 2 sec. I click again and it trace "hello" in 5 sec. I click a third time, and it trace "hello" in 3 sec... etc... I tried to write this, to try to choose a delay between 2 sec and 8 sec :

var zedelais:Array = ["2000", "3000", "4000", "5000", "6000", "7000", "8000"];

var delaisok:Number = zedelais[Math.floor(Math.random()*zedelais.length)];

var zeTimer:Timer = new Timer(delaisok,1);

//var zeTimer:Timer = new Timer(5000,1);


azer.addEventListener(MouseEvent.CLICK, azergo);

     function azergo(event:MouseEvent):void {

     zeTimer.start();

     }

zeTimer.addEventListener(TimerEvent.TIMER, timerFunction);

     function timerFunction(event:TimerEvent):void{

     trace("Hello world !!");

     }

But it doesn't work like i want. The delay change only when the program is launch, not when i click on the button...

Any idea, please?

Thanks for your help

TOPICS
ActionScript
736
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Nov 04, 2015 Nov 04, 2015

The reason it does not change is because you never tell it to.  You set it when the program starts and never change it.   Try adding the delay change in the click event handler function...

function azergo(event:MouseEvent):void {

        zeTimer.delay = zedelais[Math.floor(Math.random()*zedelais.length)];

        zeTimer.start();

}

Translate
LEGEND ,
Nov 04, 2015 Nov 04, 2015

The reason it does not change is because you never tell it to.  You set it when the program starts and never change it.   Try adding the delay change in the click event handler function...

function azergo(event:MouseEvent):void {

        zeTimer.delay = zedelais[Math.floor(Math.random()*zedelais.length)];

        zeTimer.start();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 04, 2015 Nov 04, 2015

Yeah it works, thank you for your help

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 04, 2015 Nov 04, 2015
LATEST

You're welcome

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