Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery停止/启动div在x分钟后刷新_Jquery_Settimeout_Partial Page Refresh - Fatal编程技术网

jquery停止/启动div在x分钟后刷新

jquery停止/启动div在x分钟后刷新,jquery,settimeout,partial-page-refresh,Jquery,Settimeout,Partial Page Refresh,我编写了一个小的jQuery聊天框,它每4秒在div中加载一个页面并引用: 单击“向下滑动并在div中加载页面并刷新”: $('#toggle').click( function () { $('#sbox').slideDown('fast'); var refreshId = setInterval(function() { $('#shout').load('shout.php?randval='+ Math.random()); }, 4000)

我编写了一个小的jQuery聊天框,它每4秒在div中加载一个页面并引用:

单击“向下滑动并在div中加载页面并刷新”:

$('#toggle').click( function () { 
    $('#sbox').slideDown('fast');
    var refreshId = setInterval(function() {
        $('#shout').load('shout.php?randval='+ Math.random());
    }, 4000);
    return false;
});
不过我猜这有点像是一个资源消耗器,所以我想如果x分钟后没有检测到任何活动,停止刷新,但一旦再次有活动,就恢复刷新

我发现了这个片段。。。 …它检查活动(鼠标移动),看起来非常适合我的需要,但似乎无法将两者结合起来

要总结我想要的内容,请单击div中的加载页面并开始刷新,x分钟后,如果没有活动停止刷新,则在检测到活动后再次开始刷新

// If theres no activity for 2 minutes do something
var isActive = true;
var isRefreshing = false;
var refreshChatboxWait = 4000;
var activityTimoutWait = 120000;
var activityTimeout = setTimeout(inactive, activityTimoutWait);

function resetActive(){
    isActive = true;
    clearTimeout(activityTimeout);
    if ( ! isRefreshing ) {
        var refreshId = setInterval(refreshChatbox, refreshChatboxWait);
    }
    activityTimeout = setTimeout(inactive, activityTimoutWait);
}

// No activity do something.
function inactive(){
    isActive = false;
}

function refreshChatbox(){
    $('#shout').load('shout.php?randval='+ Math.random());
    if ( ! isActive ) {
        clearInterval(refreshId);
    }
}

// Check for mousemove, could add other events here such as checking for key presses ect.
$(document).bind('mousemove', function(){resetActive()});

$('#toggle').click( function () { 
    $('#sbox').slideDown('fast');
    if ( ! isRefreshing ) {
        var refreshId = setInterval(refreshChatbox, refreshChatboxWait);
    }
    return false;
});

试试看。:)

像这样的东西应该可以。你应该能够使它适应你的需要。我基本上是从你链接的问题中复制代码的。这会将活动/非活动类添加到主体中。当您运行我的代码时,您将看到这一点。当页面处于活动状态时,屏幕为蓝色。这表示正在进行刷新。当屏幕为灰色时,不要费心刷新

然后在代码中检查活动/非活动类

var刷新率=4000;
var-inactivitytime=15000;
var-refreshid//设置为全局范围,以便以后可以清除它
var-activityTimeoutId//存储非活动超时
$('#sbox').slideUp();
$(“#切换”)。单击(函数(){
$('sbox')。向下滑动('fast');
refreshId=setInterval(函数(){
如果($('body.inactive')。长度==1){
$(“#sbox”).append('No refresh');
}
否则{
log(“刷新数据”);
$(“#sbox”).append('Refreshed…');
//$('#shout').load('shout.php?randval='+Math.random());
}
},刷新率);
返回false;
});
//如果X秒内没有活动,做点什么
activityTimeoutId=setTimeout(不活动,不活动时间);
函数resetActive(){
$(document.body).attr('class','active');
clearTimeout(activityTimeoutId);
activityTimeoutId=setTimeout(不活动,不活动时间);
}
//没有活动做某事。
函数不活动(){
$(document.body).attr('class','inactive');
}
//检查mousemove,可以在此处添加其他事件,如检查按键等。
$(document).bind('mousemove',function(){
resetActive()
});​

刚刚更新了我的答案。忘了在我的变量中替换。谢谢你,Herman先生,为了满足我自己的需要,我做了一些调整(很明显),但是工作很好,标记为答案:)做了我想要的,但是我得到了主要的页面加载延迟,并且影响了浏览器(FireFox)中打开的所有选项卡(不仅仅是我的页面),有时我甚至不得不打开任务管理器来终止FF进程:(可能是因为它设置了间隔的多个实例-是否立即尝试?
var refreshrate = 4000;
var inactivitytime = 15000;
var refreshid; //set to global scope so that you can clear it later
var activityTimeoutId; //store timeout for inactivity
$('#sbox').slideUp();

$('#toggle').click(function() {
    $('#sbox').slideDown('fast');
    refreshId = setInterval(function() {
        if ($('body.inactive').length == 1) {
            $('#sbox').append('<div>No refresh</div>');
        }
        else {
            console.log('Refreshing data');
            $('#sbox').append('<div>Refreshed...</div>');
            //$('#shout').load('shout.php?randval=' + Math.random());
        }
    }, refreshrate);
    return false;
});

// If theres no activity for X seconds do something
activityTimeoutId = setTimeout(inActive, inactivitytime);

function resetActive() {
    $(document.body).attr('class', 'active');
    clearTimeout(activityTimeoutId);
    activityTimeoutId = setTimeout(inActive, inactivitytime);
}

// No activity do something.


function inActive() {
    $(document.body).attr('class', 'inactive');
}

// Check for mousemove, could add other events here such as checking for key presses ect.
$(document).bind('mousemove', function() {
    resetActive()
});​