Solved

Call Function Sequence Randomly

Posted on 2008-09-29
Medium Priority
297 Views

``````/* Start from panel 1 */
_root.show1();

var fnArray = [show1, show2, show3, show4];
var counter = 0;
var intervalId;
function callfunctions() {
fnArray[counter]();
counter = counter+1<fnArray.length ? counter+1 : 0;

}

function startTimer(num) {
clearInterval(intervalId); /* Cleared interval, because clicking each button would offset the timer */
counter = num; /* Removed "-1" because of odd jump */
intervalId = setInterval(callfunctions, 5500);
}

/* Also removed stopTimer(); because we're calling startTimer(); in each button */

_root.startTimer(1); /* Starts the timer for panel 1 */
``````
0
Question by:webrhp
1 Comment

LVL 34

Accepted Solution

Aneesh Chopra earned 2000 total points
ID: 22597264
below is the updated code:
``````
var fnArray = [show1, show2, show3, show4];
var counter = 0;
var intervalId;
function callfunctions() {

var rnd = random(fnArray.length);
if (rnd == counter) {
callfunctions();
return;
}
fnArray[rnd]();
counter = rnd;
}
function startTimer() {
clearInterval(intervalId);
intervalId = setInterval(callfunctions, 5500);
}
callfunctions();
startTimer();

function show1() {
trace('show1');
}

function show2() {
trace('show2');
}

function show3() {
trace('show3');
}

function show4() {
trace('show4');
}
``````
0

Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.