1 Reply Latest reply on Mar 3, 2010 3:20 PM by Ned Murphy

    Randomly fire functions

    prodigalmaster Level 2

      Hi, if I make lets say 4 functions:

       

      test1();

      test2();

      something();

      blah();

       

      How would I get them to randomly fire off every few seconds?

       

      Overall, I have many movieclips, and when a particular functions goes off, the particular movieclip moves on to the screen from the right. I want these movieclips to appear on screen randomly, each with a distance of about 4 seconds.

        • 1. Re: Randomly fire functions
          Ned Murphy Adobe Community Professional & MVP

          Here's one approach.  It uses setInterval to execute callRandomFunction every 4 seconds.

           

          function func1(){
                trace("function 1 executed");
          }

           

          function func2(){
                trace("function 2 executed");
          }

           

          function func3(){
                trace("function 3 executed");
          }

           

          function func4(){
                trace("function 4 executed");
          }

           

          setInterval(callRandomFunction, 4000);

           

          function callRandomFunction(){
                var rand = Math.floor(Math.random()*4); // 4: number of functions
                if(rand == 0){
                      func1();
                } else  if(rand == 1){
                      func2();
                } else  if(rand == 2){

                      func3();
                } else {
                      func4();
                }
          }