tags:

views:

73

answers:

2

I have the following array

var arr = ['abc', 'efg', 'igj', 'feeu', 'fee'];

I want to do the following two tasks:

Loop through this array and display the text in a div called DIVCont one by one in a continuous manner and a cycle. There should be a gap of two seconds for each word.

My second requirement is to be able to click on the div called DIVClick to display the next word in the array. Each click will fetch the next word in a cycle.

I do not want to use any extra plugin for this.

A: 
<script type="text/javascript">
var arr = ['abc', 'efg', 'igj', 'feeu', 'fee'];
var index = arr.length;

$(function() {
    $('div#DIVClick').click(updateDiv);
    setInterval(updateDiv, 2000);
});

function updateDiv() {
    $('div#DIVCont').html(arr[(index++) % arr.length]);
}
</script>
Darin Dimitrov
Will this behave correctly when (say) I click on it at 3 seconds? It will have changed at 2 seconds, and then when I clicked on it at 3 seconds. Will the next change be at 4 or 5 seconds? I believe 5 is what the OP would like.
Mike Cooper
Thanks Does this loop? After the array reaches end, I want it to start again from beginning like a loop.
Yes it does loop, and yes it will start again from the beginning when it reaches the end, but I think the time you wasted posting questions you could have created an HTML page and tested the code yourself and see if it fits your needs.
Darin Dimitrov
thanks yes i should try
A: 

Yes it does loop, and yes it will start again from the beginning when it reaches the end, but I think the time you wasted posting questions you could have created an HTML page and tested the code yourself and see if it fits your needs.

I think the next time you write a long winded cranky response you could just say yes.

Dave