2012-06-10 25 views
0

我想显示一个html文件。然后淡入另一个。这是我的代码,它除了第一次,它等待10秒切换。然后在交换机之后,它在交换机之间等待5秒钟。我有点困惑,如何jQuery的处理超时和等待。我想每个开关等待5秒钟,从第一个开始。使用jquery加载一个html文件,延迟,然后另一个,循环

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script> 
$(document).ready(function() { 
     $("#responsecontainer3").load("ad1.html"); 
    var refreshId = setInterval(function() { 
    $("#responsecontainer3").fadeTo("slow").load('ad1.html?randval='+ Math.random()); 

setTimeout(function() 
    { 
    $("#responsecontainer3").fadeTo("slow").load('ad2.html?randval='+ Math.random()); 
    }, 5000); 

}, 10000); 


}); 
</script> 
<div id="responsecontainer3"> 
</div> 

回答

0
<script> 
$(document).ready(function() { 
     $("#responsecontainer3").load("ad2.html"); 

    var index = 1; 
    var refreshId = setInterval(function() { 
     $("#responsecontainer3").load('ad'+index+'.html'); 
     index = (index == 2)? 1 : index+1; 
    }, 5000); 
    $.ajaxSetup({ cache: false }); 
}); 
</script> 
0
function loadFile(url) { 
    $("#responsecontainer3").load(url, function() { 
     var index = parseInt(url.replace('ad','').replace('.html','')); 
     $("#responsecontainer3").fadeTo('slow', function() { 
     setTimeout(function() { 
      index++; 
      loadFile('ad'+index+'.html'); 
     }, 5000) 
     }); 
    }); 
} 

loadFile('ad1.html'); 
+0

什么ad2.html ... –

+0

@JoshBond检查更新答案” – thecodeparadox

+0

@codeparadox:谢谢,它加载ad1.html但从来没有加载ad2.html。我将代码放在$(document).ready(function()中。 –