Solved

increment through an array from a random index with setInterval()

Posted on 2008-06-24
Medium Priority
251 Views
Hello can anyone help, I'm trying to cycle through an array sequentially, but from a random index.

ie. if index 8 is picked by random then it would go 9, 10, 11 etc..

Ideally I need a setInterval between moving to the next index!

Cheers, P
0
Question by:Sideas

LVL 12

Accepted Solution

SamuelRostol earned 2000 total points
ID: 21854396
Hi there,

I'll post three blocks of code, one shows how to find random startindex, the second shows the setInterval-code, and the third contains the actual function :) Look below!

Good luck!
Kindly,
Samuel
``````/// Array used for testing
var aArray :Array = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k");

/// #1 Random Initiater
var iRandomNum = Math.floor(Math.random() * aArray.length); // Finds random index from aArray;

/// #2 setInterval
var iIntervalID;
var iIntervalTime :Number = 200; // 200 milliseconds between each trigger

iInterval = setInterval(doFunction, iIntervalTime);

/// #3 The actual function;
function doFunction()
{
// Place code here
trace(aArray[iRandomNum++]);

// Code to end interval when it has looped through the entire interval
if (iRandomNum >= aArray.length) {
clearInterval(iInterval);
}
}
``````
0

LVL 2

Author Closing Comment

ID: 31470067
That's a great solution - you are the man!
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.