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

Posted on 2008-06-24
Medium Priority
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
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!
/// 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
	// Code to end interval when it has looped through the entire interval
	if (iRandomNum >= aArray.length) {

Open in new window


Author Closing Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
Suggested Courses

624 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