像这样的事情应该这样做。您应该能够使其适应您的需求。我基本上从您链接的 SO 问题中复制了代码。这会在正文中添加一个活动/非活动类。当您运行我的代码时,您会看到这一点。当页面处于活动状态时,屏幕为蓝色。这表明发生了刷新。当屏幕为灰色时,请不要费心刷新。
然后,您只需检查代码中的活动/非活动类。
http://jsfiddle.net/scgEs/1
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()
});