使用jQuery加载一个HTML文件,延迟,然后另一个,循环 [英] Using Jquery to load one html file, delay, then another, looping

查看:124
本文介绍了使用jQuery加载一个HTML文件,延迟,然后另一个,循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个html文件.然后淡出另一个.这是我的代码,它可以正常工作,除了第一次,它需要等待10秒钟才能切换.然后,在切换之后,它会在切换之间等待5秒钟.我对如何使用jquery的句柄来处理超时和等待感到困惑.我想每次切换都要等待5秒钟,从第一个开始.

I'd like to show one html file. Then fade to another one. Here's my code and it works, except the first time, it waits 10 seconds to switch. Then after the switch, it waits 5 seconds in between switches. I'm a little confused as to how to jquery's handles timeouts and waits. I'd like to wait 5 seconds per switch, beginning with the first one.

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

推荐答案

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

这篇关于使用jQuery加载一个HTML文件,延迟,然后另一个,循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆