Aide en changement d'images en javascript

Résolu/Fermé
natasha - 8 févr. 2012 à 21:02
 natasha - 10 févr. 2012 à 00:08
Bonjour,

j'ai crée un script qui change les images après 3 secondes mais ça marche pas même logiquement tous s'apparait normal voici le code

<script type="text/javascript">
function godiapo()
{

var images = new Array("2.jpg","3.jpg","4.jpg");
var photo = document.getElementById('pico');
for(var i = 0; i<images.length; i++)
{
setTimeout(function (){
photo.src = images[i];},3000);
}

}
</script>
</head>

<body onload="godiapo()">
<img src="2.jpg" width="300" height="200" id="pico"/>
</body>
</html>

Merci

A voir également:

1 réponse

Salut,
pas suffisant un setTimeout puisque comme son nom l'indique permet de régler un temps d'arrêt.

Il faut utiliser Set/ClearInterval


Un exemple:
http://www.toutjavascript.com/reference/reference.php?iref=206
0
si si setTimeout est suffisant
j'ai trouvé une astuce

<script type="text/javascript">



var images=new Array("1.jpg","2.jpg","3.jpg","4.jpg");
var i=0;
function changeimages()
{
document.getElementById("ima").src=images[i];
i++;
if(i>=images.length) i=0;
setTimeout("changeimages()",3000);
}

</script>
</head>

<body onload="changeimages()">
<img src="" width="300" height="200" id="ima"/>
</body>
0