我一直在研究并发现代码可以在用户闲置一段时间后检测不活动(当用户不触摸屏幕时),这在使用鼠标时效果很好,但当我尝试通过触摸使用它时屏幕设备,它无法检测到我的手指或工作。我添加了很多 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");
}
}
最佳答案
我会调整时间(你说你想要每分钟更新一次,但你将 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");
}
}
关于javascript - 使用 JavaScript 检测触摸设备上的不活动状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52649058/