Onmouseover数据保持autorefresh

时间:2012-08-15 07:20:40

标签: php javascript jquery

我的代码如下:

$(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,数据会保持自动刷新吗?

2 个答案:

答案 0 :(得分: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);
    });

答案 1 :(得分:0)

我对您的问题感到有点困惑,以下代码负责在mouseover

时停止自动刷新
$("#content2").bind("mouseover", function() {
    clearInterval(refreshInterval); // it's clearing the setInterval
});

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

但如果您只想在mouseover上进行auti刷新并再次想要停止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);
*/

但您可以将变量declarede保留为var refreshInterval=0;