• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

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

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
Sideas
Asked:
Sideas
1 Solution
 
SamuelRostolCommented:
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
 
SideasAuthor Commented:
That's a great solution - you are the man!
0
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

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now