PDA

View Full Version : Whats going wrong?



scooby545
11-20-2008, 11:48 AM
Basically i am trying to get this script to cycle through the images in the array in a loop with a timed delay :confused:

The script cycles the images through fine but will not reset back to the first image :( I just cant figure out whats going wrong and its driving me mad :mad:

Please help!! :confused:


<html>

<head>
<title>Testing Page</title>

<script type="text/javascript">

var myCounter = 0;
var myArray = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg'];
var arraylen = myArray.length;

function changepic()
{
document.getElementById('test').innerHTML = 'Counter : ' +myCounter + '<br>Array : ' +arraylen + '<br><img src="' +myArray[myCounter] + '">';
myCounter++;
}

function changepic2()
{
var myCounter = 0;
document.getElementById('test').innerHTML = 'Counter : ' +myCounter + '<br>Array : ' +arraylen + '<br><img src="' +myArray[myCounter] + '">';
}

function alertme()
{
if(arraylen > myCounter){
intervalID = setInterval(changepic, 5000);
}else{
intervalID = setInterval(changepic2, 5000);
}
}

</script>

</head>

<body bgcolor='#ffffff' onload="alertme();">

<div id='test'>
<img src="image1.jpg">
</div>

</body>

</html>

jscheuer1
11-20-2008, 03:50 PM
Without correcting a lot of things that are non-standard about your demo code:


<html>

<head>
<title>Testing Page</title>

<script type="text/javascript">

var myCounter = 0;
var myArray = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg'];
var arraylen = myArray.length;

function changepic()
{
if(arraylen > myCounter){
document.getElementById('test').innerHTML = 'Counter : ' +myCounter + '<br>Array : ' +arraylen + '<br><img src="' +myArray[myCounter] + '">';
myCounter++;
}
else {
myCounter = 0;
changepic();
}
}

function alertme()
{
intervalID = setInterval(changepic, 5000);
}

</script>

</head>

<body bgcolor='#ffffff' onload="alertme();">

<div id='test'>
<img src="image1.jpg">
</div>

</body>

</html>

should (untested) take care of it. The basic problem in your version is that:


function alertme()
{
if(arraylen > myCounter){
intervalID = setInterval(changepic, 5000);
}else{
intervalID = setInterval(changepic2, 5000);
}

only runs once, and that even if it ran continually and started setting various different intervals, things would fairly quickly get all jumbled up with several intervals playing out concurrently, some even simultaneously.

scooby545
11-20-2008, 06:00 PM
kinda new to javascript lol :)

thanks

ggerri
11-20-2008, 11:15 PM
Hi Scooby 545

I suggest you check out jquery.com It makes JavaScript life so much easier. There're many plugins which are available to offer you the functions you need. Maybe http://sorgalla.com/projects/jcarousel/#Examples ?

Regards
ggerri