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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀