Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
182
rated 0 times [  184] [ 2]  / answers: 1 / hits: 15894  / 11 Years ago, thu, march 28, 2013, 12:00:00

I am trying to essentially create a ticker which shows a single string item taken from an array at a time. Basically I just want it to show a single item and then transition into the next, my Javascript skills are massively rudimentary (Jquery might be better for this.)



Here is my code:



var title= ['Orange', 'Apple', 'Mango', 'Airplane', 'Kiwi'];
for (var i=0; i<title.length; i++){
document.write(title[i]);
}


What do I have to add?



Thanks a bunch!


More From » arrays

 Answers
4

Start by learning about document.getElementById and setInterval.



http://jsfiddle.net/wtNhf/



HTML:



<span id=fruit></span>


Javascript:



var title = ['Orange', 'Apple', 'Mango', 'Airplane', 'Kiwi'];

var i = 0; // the index of the current item to show

setInterval(function() { // setInterval makes it run repeatedly
document
.getElementById('fruit')
.innerHTML = title[i++]; // get the item and increment i to move to the next
if (i == title.length) i = 0; // reset to first element if you've reached the end
}, 1000); // 1000 milliseconds == 1 second

[#79258] Wednesday, March 27, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
wilson

Total Points: 27
Total Questions: 93
Total Answers: 93

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
wilson questions
Tue, Aug 9, 22, 00:00, 2 Years ago
Wed, May 11, 22, 00:00, 2 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Wed, May 13, 20, 00:00, 4 Years ago
;