Solved

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

Posted on 2008-06-24
2
237 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
Comment Utility
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
Comment Utility
That's a great solution - you are the man!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
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…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

771 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

11 Experts available now in Live!

Get 1:1 Help Now