如何在前端制作 "NO ACTION" 检测器?
How to make "NO ACTION" detector on frontend?
当您进入 codecademy 并在一定时间内无所事事时,
你会看到消息说 "Are you still there?".
我也想在我的网页上实现这个功能。
好吧,首先想到这个想法,
使用 cookie 或会话将是最好的方法?
或者有什么好主意吗?
也许不是最优雅的解决方案,但我认为它可以满足您的需求。
$(function() {
var interval;
var startTime = new Date();
var checkTime = function () {
var endTime = new Date();
var elapsed = endTime - startTime;
var seconds = Math.round(elapsed / 1000);
var minutes = Math.round(seconds / 60);
console.log("seconds since mouse move: " + seconds);
if (seconds >= 5) {
console.log("Are you still there?");
clearInterval(interval);
}
};
interval = setInterval(checkTime, 1000);
$(document).on('mousemove', function(e) {
console.log('mouse moved');
startTime = new Date();
clearInterval(interval);
interval = setInterval(checkTime, 1000);
});
});
当您进入 codecademy 并在一定时间内无所事事时, 你会看到消息说 "Are you still there?".
我也想在我的网页上实现这个功能。 好吧,首先想到这个想法, 使用 cookie 或会话将是最好的方法? 或者有什么好主意吗?
也许不是最优雅的解决方案,但我认为它可以满足您的需求。
$(function() {
var interval;
var startTime = new Date();
var checkTime = function () {
var endTime = new Date();
var elapsed = endTime - startTime;
var seconds = Math.round(elapsed / 1000);
var minutes = Math.round(seconds / 60);
console.log("seconds since mouse move: " + seconds);
if (seconds >= 5) {
console.log("Are you still there?");
clearInterval(interval);
}
};
interval = setInterval(checkTime, 1000);
$(document).on('mousemove', function(e) {
console.log('mouse moved');
startTime = new Date();
clearInterval(interval);
interval = setInterval(checkTime, 1000);
});
});