简体   繁体   English

jQuery自动刷新div故障

[英]jQuery auto refresh div malfunctioning

Does anyone know what I'm doing wrong? 有人知道我在做什么错吗?

Basically I'm after loading content (an image) into a div tag, and then refreshing it every x seconds. 基本上,我是在将内容(图像)加载到div标签后,然后每隔x秒刷新一次。 So this is what I came up with, and it works great until it fades the file back in to which it does a manual refresh. 因此,这就是我想出的方法,它可以很好地起作用,直到将文件淡入并手动刷新之前。

The current process looks like the following: 当前过程如下所示:

Load ... fade out, fade in & then disappears and reappears a few seconds later. 载入...逐渐消失,逐渐消失,然后消失,然后在几秒钟后重新出现。

What should be happening is the following: 应该发生以下情况:

Load ... fade out, fade in ... fade out, fade in ... (you get the idea, looped) 载入...淡出,淡入...淡出,淡入...(您明白了,循环了)

The code: 编码:

$(document).ready(function () {
     $("#webcam").load("image.html").fadeIn("slow");
   var refreshId = setInterval(function() {
      $("#webcam_img").fadeOut("slow");
      $("#webcam").load('image.html');
      $("#webcam_img").fadeIn("slow");
   }, 5000);
});

The online file can be found here: http://colourednoise.co.uk/scripts/webcam.htm 可以在这里找到在线文件: http : //colourednoise.co.uk/scripts/webcam.htm

Thank you 谢谢

Try moving your fadein inside the callback function for the fadeOut so it waits until fadeout is complete first 尝试将淡入移入淡出的回调函数中,以便等到淡出先完成

   $("#webcam_img").fadeOut("slow",function(){
      $("#webcam").load('image.html');
      $("#webcam_img").fadeIn("slow");   
   });

Here's the full example 这是完整的例子

$(document).ready(function () {
   $("#webcam").load("image.html").fadeIn("slow");
   var refreshId = setInterval(function() {
      $("#webcam_img").fadeOut("slow",function(){ // <-- added callback function
         $("#webcam").load('image.html'); // now this
         $("#webcam_img").fadeIn("slow"); // and this waits for fadeOut to complete first  
      });
   }, 5000);
});

load is asynchronous. load是异步的。 You are calling fadeIn on an element which is then replaced by the load . 您正在对元素调用fadeIn ,然后将其替换为load You need to fadeIn within the callback of load to ensure that the element exists. 您需要fadeIn的回调中load ,以确保该元素存在。

$("#webcam").load('image.html', function(){
    $("#webcam_img").fadeIn("slow");
});

Here is a slightly different way to do it, ensuring that each image is fully loaded before the next refresh gets queued: 这是一种稍微不同的方法,可确保在下次刷新进入队列之前,每个映像都已完全加载:

$(document).ready(function () {
    $("#webcam").load("image.html").fadeIn("slow", function () { setTimeout(refreshImage, 5000) });

    function refreshImage() {
        $("#webcam_img").fadeOut("slow",function(){
            $("#webcam").load('image.html');
            $("#webcam_img").fadeIn("slow", function() { setTimeout(appendDateToBody, 5000) });
        });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM