setTimeout stalls while loop
I set up a slide show (Slideshow()) using setTimeout, and it works fine. I need to limit the slide show to 3 repeats, but when I add a while loop (Count()) it it prints Test 1 and stalls
function SlideShow()
{
setTimeout("document.write('Test 1')", 1500);
setTimeout("document.write('Test 2')", 3000);
setTimeout("docum开发者_StackOverflowent.write('Test 3')", 4500);
}
function Count()
{
var i=0;
do
{
SlideShow();
i++;
}
while (i<=3);
}
This works for me:
function slideShow() {
setTimeout("alert('1');", 1500);
setTimeout("alert('2');", 3000);
setTimeout("alert('3');", 4500);
}
function count() {
for(var i=0; i<3; i++) {
slideShow();
}
}
count();
You could just use one timeOut like in:
<img id="theImg" />
<script>
var cnt = 2,
i = 0,
pics = [
'image1.png',
'image2.png',
'image3.png'
];
function SlideShow(ap){
if(ap[i]){
//set the src of theImg to the item i of the array
document.getElementById('theImg').src = ap[i++];
setTimeout(function(){
SlideShow(ap);
}, 1500);
}else if(cnt--){
i = 0;
SlideShow(pics);
}
}
SlideShow(pics);
</script>
精彩评论