我一直在研究并发现在用户空闲x个时间后检测不活动的代码(当用户没有触摸屏幕时),这在使用鼠标时工作得很好,但当我尝试在触摸屏设备上使用它时,它无法检测到我的手指或工作。我已经添加了很多DOM事件,比如"touchstart“、"touchmove”、"touchend“、"touchcancel”等等,但它们似乎也不起作用。以下是我的代码
var idleTime = 0;
$(document).ready(function () {
//Increment the idle time counter every minute.
var idleInterval = setInterval(timerIncrement, 3000);
//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
});
function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime > 1) {
alert("ok");
}
}
发布于 2018-10-04 22:38:42
我将调整计时器(您说您希望每分钟更新一次,但您将setInterval设置为3000毫秒,我将尝试这样做:
var idleTime = 0;
$(document).ready(function () {
//Increment the idle time counter every minute.
var idleInterval = setInterval(timerIncrement, 60000);
//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
//Zero the idle timer on touch events.
$(this).bind('touchstart', function(){
idleTime = 0;
});
$(this).bind('touchmove', function(){
idleTime = 0;
});
});
function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime > 1) {
alert("ok");
}
}
发布于 2018-10-04 22:27:31
尝试添加此代码:
$(this).bind('touchstart touchmove click', function(){ idleTime = 0; }
https://stackoverflow.com/questions/52649058
复制相似问题