Solved

Array next object

Posted on 2013-10-22
4
263 Views
Last Modified: 2013-11-19
Hello expert,

Thought I could get the next object in an array by
calling the array with [+1} next to it. But its not working.
How can I get the next array item. Full code below.

Thanks.

Allen in Dallas


<html>
<head>
<style type="text/css">

 body {font-family: arial;}
 </style>
<script type="text/javascript">
function ChangeIt()
{
var colors;
colors = new Array("red", "blue", "green", "yellow", "purple");

document.body.style.backgroundColor = colors[0];

var t = setInterval(function() {
    document.body.style.backgroundColor = colors[+1];
}, 3000);

}
</script>
</head>
<body>
This page begins with a red background and
changes the body background after three seconds.

The Javascript
function is set in header section and called
from the body.
</body>
<script type="text/javascript">
ChangeIt();
</script>
</body>
</html>
0
Comment
Question by:9apit
  • 3
4 Comments
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 500 total points
ID: 39592768
That will get blue and continue to get blue forever.  

You need to set an index variable and increment it:
document.body.style.backgroundColor = colors[0];
i=0;
var t = setInterval(function() {
    i=(i>=color.length) ? 0 : i+1;
    document.body.style.backgroundColor = colors[i];
}, 3000);

Open in new window



Cd&
0
 

Author Comment

by:9apit
ID: 39593983
Hello COBOL dinasaur,

Thanks for the help.
I inserted your excellent script
into the page with no result.

I'll bet its just a syntax error but
I can't find it.

Thanks

Allen in Dallas
change-j.html
0
 

Author Comment

by:9apit
ID: 39594014
Hello COBOL,

I found it
i=(i>=color.length) ? 0 : i+1;
should be
i=(i>=colors.length) ? 0 : i+1;

Works now

Thanks.

Allen
0
 

Author Closing Comment

by:9apit
ID: 39594022
Thanks for the excellent answer.

in the expression
 i=(i>=colors.length) ? 0 : i+1;

It says if I is greater than the length of the array then set i back to 1
Correct?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to count occurrences of each item in an array.

910 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

22 Experts available now in Live!

Get 1:1 Help Now