简体   繁体   English

如何将setInterval与mouseenter结合使用?

[英]how to combine setInterval with mouseenter?

I am trying to make it so a div refreshes with ajax. 我正在尝试使它使用ajax刷新div。 The code itself is implemented and working already. 该代码本身已实现并且已经在工作。 I want the div to refresh every 30 seconds but only on an active tab. 我希望div每30秒刷新一次,但仅在活动选项卡上刷新。 From what I understand setInterval will refresh every time regardless of whether the tab is in use or not. 据我了解,无论该选项卡是否在使用中,setInterval都会每次刷新。 Id like to combine a mouseenter (or some other kind of user interaction that implies the user is active on the site) with setInterval so that the setInterval doesnt get fired if inactive. 我想将mouseenter(或其他暗示用户在站点上处于活动状态的用户交互)与setInterval结合使用,以使setInterval在不活动时不会被触发。

currently I have this code which works well on the initial page load. 目前,我有此代码在初始页面加载时效果很好。 There is no refresh during the first 30 seconds, nor is there a refresh until mouseenter on the div. 在开始的30秒内没有刷新,直到div上的mouseenter都没有刷新。 However after the initial 30 seconds it refreshes on every mouseenter. 但是,在最初的30秒后,它会在每个mouseenter上刷新。

$(document).ready(function(){

    $("#container").hide();
    $("#loading").show();
    $("div#refresh").load('example.com/load.php', function(){ $("#container").show(); $("#loading").hide(); });

    function refresh() {
        $("#refresh").mouseenter(function() {
        //  $("#container").hide();
            $("#loading").show();
            $("div#refresh").load('example.com/load.php', function(){ $("#container").show(); $("#loading").hide(); });
        });
        clearInterval(intervalID);
    }

    var intervalID = setInterval(refresh, 30000); // Will alert every 30 second.
    // clearInterval(intervalID); // Will clear the timer.

});

Just set the interval when the mouse cursor is in the tab you want, and clear it when it's outside: 只需在鼠标光标位于所需选项卡中时设置时间间隔,并在其位于外部时清除它:

var intervalID, lastRefresh = 0;
$("#refresh").mouseenter(function() {
    var diff = new Date().getTime() - lastRefresh;
    intervalID = setTimeout(function() {
        refresh();
        intervalID = setInterval(refresh, 30000);
    }, 30000 - diff);
}).mouseleave(function() {
    clearInterval(intervalID);
});

function refresh() {
    $("#loading").show();
    $("div#refresh").load('example.com/load.php',
            function(){ $("#container").show(); $("#loading").hide(); });
    lastRefresh = new Date().getTime();
}

Now the <div> is refreshed in the instant the mouse enters inside its borders, and every 30 seconds from that moment. 现在, <div>会在鼠标进入其边界的瞬间以及从此刻起每30秒刷新一次。 This stops when the mouse leaves the <div> . 当鼠标离开<div>时,此操作停止。

If the mouse enters again, it checks for the last time the refresh function was called. 如果鼠标再次进入,它将检查上一次调用refresh功能的时间。 If less than 30 seconds have passed, it waits until 30 seconds pass. 如果少于30秒,则等待30秒。

Pro-tip : clearInterval also clears timed events generated by setTimeout , just like clearTimeout cancels setInterval . 提示clearInterval还会清除setTimeout生成的定时事件,就像clearTimeout取消setInterval

Try this: 尝试这个:

$(document).ready(function(){
    var intervalID;
    $("#container").hide();
    $("#loading").show();
    $("div#refresh").load('example.com/load.php', function(){ $("#container").show(); $("#loading").hide(); });

function refresh() {
        //  $("#container").hide();
            $("#loading").show();
            $("div#refresh").load('example.com/load.php', function(){ $("#container").show();     $("#loading").hide(); });
    }

 $("#refresh").mouseenter(function() {
     intervalID= setInterval(refresh, 30000); 
  });

$("#refresh").mouseleave(function() {
  clearInterval(intervalID);
 });
$(function(){
    var intervalID;
    $("#container").hide();
    refresh();

    function refresh() {
        $("#loading").show();
        $("div#refresh").load('example.com/load.php', function(){
            $("#container").show(); 
            $("#loading").hide(); 
        });
    }
    $("#refresh").on({
        mouseenter: function() {
            intervalID = setInterval(refresh, 30000);
        },
        mouseleave: function() {
            clearInterval(intervalID);    
        }
    });
});

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

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