2010-08-17 327 views
0

我使用setTimeout设置幻灯片放映(幻灯片放映()),它工作正常。我需要在幻灯片放映限制为3次重复,但是当我添加一个while循环(COUNT()),它它打印测试1和摊位setTimeout停止while循环

function SlideShow() 
{ 
    setTimeout("document.write('Test 1')", 1500); 
    setTimeout("document.write('Test 2')", 3000); 
    setTimeout("document.write('Test 3')", 4500); 
} 

function Count() 
{ 
    var i=0; 

    do 
    { 
    SlideShow(); 
    i++; 
    } 
    while (i<=3); 
} 

回答

0

这个工作对我来说:

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(); 
+0

(谷歌浏览器) – 2010-08-17 06:10:11

+0

感谢您的努力 - 它实际上重复alert1的3倍,那么警报2〜3次,等等 - 对不起 - Rhys – Rhys 2010-08-17 06:25:53

+0

你想要什么?这就是你上面描述的*“我需要限制它3次重复”* – 2010-08-17 06:46:12

0

你可能只是在使用一个超时,如:

<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> 
+0

嘿 - 这是辉煌的,作品 – Rhys 2010-08-17 12:25:20

+0

我需要添加一个字符串包含补间程序后的计时器(在},1500);)。我有变量var1,var2等 中的例程,如“tween ='var'+ i; echo tween;”。 语法似乎是错误的,因为它不会运行。 如果我手动输入一个补间字符串,它可以正常工作,但我需要将每个例程匹配到适当的幻灯片图片。 – Rhys 2010-08-18 02:32:49

+0

明白了,为补间使用了另一个数组 – Rhys 2010-08-18 06:34:32