Jquery推荐旋转器Ajax函数早期运行

Jquery推荐旋转器Ajax函数早期运行,jquery,html,rotator,Jquery,Html,Rotator,我正在为我的网站制作一个带有Jquery的推荐旋转器,但是Ajax功能运行得太早了,所以你会看到HTML的变化,然后它会消失,然后重新出现。我试图找出这个问题,但似乎找不到 <div class="testimonials"> <div id="testimonial"></div> </div> <script type="text/javascript"> function loadTestimonial(){

我正在为我的网站制作一个带有Jquery的推荐旋转器,但是Ajax功能运行得太早了,所以你会看到HTML的变化,然后它会消失,然后重新出现。我试图找出这个问题,但似乎找不到

<div class="testimonials">
    <div id="testimonial"></div>
</div>
<script type="text/javascript">
    function loadTestimonial(){
        $("#testimonial").fadeOut(1000);
        $.ajax({
            url: "r/get_testimonial.php",
            cache: false,
            async: false,
            timeout: 1000,
            success: function(html){
                $("#testimonial").html(html);
            },
          });   
          setTimeout($("#testimonial").fadeIn(1000), 2000);
      } 
      $(document).ready(function(){
        loadTestimonial();
        setInterval (loadTestimonial, 5000) 
      });
</script>

函数loadTestional(){
美元(“#证明”)。淡出(1000);
$.ajax({
url:“r/get_estimational.php”,
cache:false,
async:false,
超时:1000,
成功:函数(html){
$(“#证明”).html(html);
},
});   
setTimeout($(“#证明”)。fadeIn(1000),2000年;
} 
$(文档).ready(函数(){
加载证明();
设定时间间隔(负载证明,5000)
});
尝试以下代码:

<div class="testimonials">
    <div id="testimonial"></div>
</div>
<script type="text/javascript">
    function loadTestimonial(){
        $("#testimonial").fadeOut(1000, function(){
            $.ajax({
                url: "r/get_testimonial.php",
                cache: false,
                async: false,
                timeout: 1000,
                success: function(html){
                    $("#testimonial").html(html).fadeIn(1000);
                },
              });   
        });
      } 
      $(document).ready(function(){
        loadTestimonial();
        setInterval (loadTestimonial, 5000) 
      });
</script>

函数loadTestional(){
$(“#证明”).fadeOut(1000,函数(){
$.ajax({
url:“r/get_estimational.php”,
cache:false,
async:false,
超时:1000,
成功:函数(html){
$(“#证明”).html(html).fadeIn(1000);
},
});   
});
} 
$(文档).ready(函数(){
加载证明();
设定时间间隔(负载证明,5000)
});
你的代码出了什么问题?ajax代码立即被调用,它将在准备就绪时替换旧的HTML,我在上面所做的是等待
fadeOut()
的回调,然后触发ajax函数。这将获得源代码和新HTML

您可能会认为它是一个同步函数(因为Ajax具有
async:false
),但jQuery
fadeOut也具有异步性,因此您不能像以前那样等待它。

尝试以下代码:

<div class="testimonials">
    <div id="testimonial"></div>
</div>
<script type="text/javascript">
    function loadTestimonial(){
        $("#testimonial").fadeOut(1000, function(){
            $.ajax({
                url: "r/get_testimonial.php",
                cache: false,
                async: false,
                timeout: 1000,
                success: function(html){
                    $("#testimonial").html(html).fadeIn(1000);
                },
              });   
        });
      } 
      $(document).ready(function(){
        loadTestimonial();
        setInterval (loadTestimonial, 5000) 
      });
</script>

函数loadTestional(){
$(“#证明”).fadeOut(1000,函数(){
$.ajax({
url:“r/get_estimational.php”,
cache:false,
async:false,
超时:1000,
成功:函数(html){
$(“#证明”).html(html).fadeIn(1000);
},
});   
});
} 
$(文档).ready(函数(){
加载证明();
设定时间间隔(负载证明,5000)
});
你的代码出了什么问题?ajax代码立即被调用,它将在准备就绪时替换旧的HTML,我在上面所做的是等待
fadeOut()
的回调,然后触发ajax函数。这将获得源代码和新HTML

您可能会认为它是一个同步函数(因为Ajax具有
async:false
),但是jQuery
fadeOut
也是异步的,所以您不能像以前那样等待