0

我在下面有这样的代码:

$(document).ready(function() {
    $("#content2").load("post_rf.php");
    // set your initial interval to kick it off
    var refreshInterval = setInterval(function() {
        $("#content2").load('post_rf.php?randval='+ Math.random());
    }, 1500);
    // bind an event to mouseout of your DIV to kickstart the interval again
    $("#content2").bind("mouseout", function() {
        refreshInterval = setInterval(function() {
            $("#content2").load('post_rf.php?randval='+ Math.random());
        }, 1500);
    });
    // clear the interval on mouseover of your DIV to stop the refresh
    $("#content2").bind("mouseover", function() {
        clearInterval(refreshInterval);
    });
    $.ajaxSetup({
        cache: false
    });
});

我想做的是什么时候mouseover,数据保持自动刷新。在这种情况下,如果我将鼠标拖到 的区域mouseover,它会停止自动刷新,直到我拖出mouseout的区域mouseover

那么是否可以设置onmouseover,数据将保持自动刷新?

4

2 回答 2

1

我认为你想要的是相反的方式,

    $("#content2").bind("mouseover", function() {
        refreshInterval = setInterval(function() {
            $("#content2").load('post_rf.php?randval='+ Math.random());
        }, 1500);
    });
    // clear the interval on mouseover of your DIV to stop the refresh
    $("#content2").bind("mouseout", function() {
        clearInterval(refreshInterval);
    });
于 2012-08-15T07:37:31.780 回答
0

我对你的问题有点困惑,下面的代码负责在你停止自动刷新时mouseover

$("#content2").bind("mouseover", function() {
    clearInterval(refreshInterval); // it's clearing the setInterval
});

如果您只是删除/禁用该行或整个功能,那么当您在该元素上移动鼠标时它不会停止自动刷新。

但是,如果您只想发生自动刷新,mouseover并且再次想要停止自动刷新,mouseout那么您可以执行以下操作

$("#content2").bind("mouseover", function() {
    refreshInterval = setInterval(function() {
        $("#content2").load('post_rf.php?randval='+ Math.random());
    }, 1500);
});

$("#content2").bind("mouseout", function() {
    clearInterval(refreshInterval);
});

并且最初不应使用以下代码启动自动刷新,只需禁用代码如下

/*
var refreshInterval = setInterval(function() {
    $("#content2").load('post_rf.php?randval='+ Math.random());
}, 1500);
*/

但是您可以将声明的变量保留为var refreshInterval=0;

于 2012-08-15T07:37:58.370 回答