[英]How can I write a javascript function that moves between my tabs on a html webpage every few seconds if the user is idle
我从优雅地检测 JavaScript 中的空闲时间中获得了以下代码的基础
但除非在我访问网页后有鼠标移动,否则它并没有真正起作用。 如果我想通过单击菜单栏加载下一个 html 文件,然后稍微移动鼠标,然后在 3 秒内什么都不做,选项卡将自动更改。 但是在那次更改之后,我仍然让它闲置,它就留在那里。 我正在尝试编写一个函数,当使用每 3 秒连续空闲时执行某些操作。 如果用户空闲,我想继续在选项卡之间切换。 我需要一个函数或使用什么方法的想法。
function idle_time(){
var t;
//document.onload = resetTimer;
window.onload = resetTimer;
//window.onloadend = resetTimer;
window.onmousemove = resetTimer;
window.onmousedown = resetTimer; // touchscreen presses
window.ontouchstart = resetTimer;
window.onclick = resetTimer; // touchpad clicks
window.onscroll = resetTimer; // scrolling with arrow keys
window.onwheel = resetTimer;
window.onkeypress = resetTimer;
window.onhashchange = resetTimer;
document.addEventListener("load",resetTimer);
// document.addEventListener("mousedown",resetTimer);
// document.addEventListener("touchstart",resetTimer);
// document.addEventListener("click",resetTimer);
// document.addEventListener("scroll",resetTimer);
// document.addEventListener("keypress",resetTimer);
function next_tab(){
var curr_window = window.location.href; // Get URL of string of webpage location
i = pages_arr.indexOf(curr_window); // Get the index of that location in the array of pages
if (i==pages_arr.length - 1){
i = 0; // If index is at last element go to first element of array
}
else{
++i; // All other cases go to next tab
}
window.location.assign(pages_arr[i]); // Load page of the URL
}
function resetTimer() {
clearTimeout(t);
t = setTimeout(next_tab, 3000)
// 1000 milisec = 1 sec
}
// var timer = 0;
// setInterval(function(){++timer;},1000);
// if (timer == 3 && !(window.onload ||window.onmousemove||window.onmousedown||window.ontouchstart||window.onclick||window.onscroll||window.onwheel||window.onkeypress)){
// timer = 0;
// next_tab();
// }
// else{
// //idle_time();
// }
}
working demo- http://jsfiddle.net/Malkeet12/6Rm9S/27/
var idleInterval = setInterval(timerIncrement, 3000);
$scope.myListener = function() {
clearInterval(idleInterval);
// console.log('idleInterval');
idleInterval = setInterval(timerIncrement, 3000); // 1 minute
};
function timerIncrement() {
var arr = document.getElementById("someFormId").elements;
for (var i = 0; i <= 3; i++) {
setTimeout(function(x) {
return function() {
arr[x].focus();
};
}(i), 1000 * i);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.