Solved

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

Posted on 2008-06-24
2
238 Views
Last Modified: 2010-05-18
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
Comment
Question by:Sideas
2 Comments
 
LVL 12

Accepted Solution

by:
SamuelRostol earned 500 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);

	}

}

Open in new window

0
 
LVL 2

Author Closing Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Inserting a pop up image to a scanned PDF 5 433
Flash media server 1 326
Open Source MP4 Flash Player 2 169
IE11 stops playing video on a certain website 10 68
This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

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

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

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now