dousha1873 2016-05-29 18:17
浏览 54
已采纳

图像旋转脚本在AJAX加载时中断,开始旋转太快

I adapted an image rotation script from here.

I have a main page (main.php), and a button that will load a page (ajax.php) into a div container on main.php. The code for image rotation is placed inside $(document).ready(function() section of the ajax.php page. This code is below.

When the page first loads, it is okay and the images are rotating once every 1000ms. If I click the button to the load ajax.php to put in a new rotating set of images, it loads two images in succession, waits 1000ms, loads two images, etc. If I click a third time, it will load three images in succession, wait 1000ms, etc. Do you have an idea where the problem is occurring? Thanks.

Just an edit - if I put a comment inside the mouseleave function I see it will get called multiple times when the problem occurs, so it could be the .mouseleave(); code at the very end causing problems?

Edit2: I have added new code snippets. One from the main.php and two from the ajax.php page:

MAIN

<link rel="stylesheet" href="css/classic.css" type="text/css" />

<style>
#main-content {
  width: 1000px;
  height: 600px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -300px 0 0 -380px;
}
</style>

</head>

<body>


<div>
    <button id="main-button">Get Random Content</button>
</div>

<div style="clear: both"></div>


<div id="main-content"></div>

</script><script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js'></script></script><script src="jquery.jeditable.js" type="text/javascript" charset="utf-8"></script><script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script><script src="jquery-ui.js"></script>


<script>
$(document).ready(function() {
  $("#main-content").load("test1_slide.php");

  $("#main-button").click(function() {
    $("#main-content").load("test1_slide.php");
  });
});

</script>


</body>
</html>

AJAX

<style>
#slide img {
    position: absolute;
    display: block;
}
#slide {
    position: relative;
}
</style>

<?php
echo '<div id="slide">';
for ($i = 0; $i < 15; $i++) {
    echo '<img src="http://placehold.it/350x150?text='.$i.'">';
}
echo '</div>';
?>

<script>
$(document).ready(function() {

    var timer;


    // adapted from http://jsfiddle.net/didierg/Cvxe2/
    $("#slide > img:gt(0)").hide();
    $("#slide")
        .mouseenter(function() {
            if (timer) {
                clearInterval(timer);

            }
        })
        .mouseleave(function() {
            timer = setInterval(function() {
                $("#slide > img:first")
                    .fadeOut(0)
                    .next()
                    .fadeIn(0)
                    .end()
                    .appendTo("#slide");
                console.log("test");
            }, 1000);
        })
        .mouseleave();



});
</script>
  • 写回答

1条回答 默认 最新

  • dongshenjie3055 2016-05-29 18:20
    关注

    Edit since more code has been disclosed

    The issue looks like it is a problem with stopping the prior setInterval(). The event handlers that would normally stop it are wiped and so it keeps going forever. And, because it's inside a $(document).ready() callback function scope, you can't get to it to manually clear it.

    I think the best way to fix it is to move the slide <script> out of PHP content and into the main page. That will then give you control over the timer variable that holds the previous setInterval() timerID so you can clear the old one before re-initializing the new content.

    Then, you can do this:

    <script>
    $(document).ready(function() {
    
    var timer;
    
    function initializeSlide() {
        clearInterval(timer);
        // adapted from http://jsfiddle.net/didierg/Cvxe2/
        $("#slide > img:gt(0)").hide();
        $("#slide")
            .mouseenter(function() {
                if (timer) {
                    clearInterval(timer);
    
                }
            })
            .mouseleave(function() {
                timer = setInterval(function() {
                    $("#slide > img:first")
                        .fadeOut(0)
                        .next()
                        .fadeIn(0)
                        .end()
                        .appendTo("#slide");
                    console.log("test");
                }, 1000);
            })
            .mouseleave();
    }
    
    $("#main-button").click(function() {
        $("#main-content").load("test1_slide.php");
        initializeSlide();
      }).click();
    });
    
    </script>
    

    Original answer

    You are apparently rerunning code that sets up new setInterval() timers each time you load new content. That gets you duplicate timers that cause the multiple unwanted actions.

    There are a bunch of possible causes for how you could end up with a duplicate timer:

    1. An event handler on replaced content may be getting removed when the new content is loaded and that keeps a prior timer from getting removed.

    2. Code for initializing the new content may be starting a new timer without stopping an old one.

    3. A variable may be getting re-initialized, thus overwriting the previous timer variable.

    4. When you initialize the newly loaded content, you add duplicate event handlers. You need to either remove prior event handlers before readding new event handler or you need to not doubly reinitialize the content.

    We would have to see more about how the HTML is laid out, what content has event handlers on it and what content is being reloaded to know exactly which problem is occurring.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用