gpt4 book ai didi

javascript - 如果用户空闲,我如何编写一个 javascript 函数每隔几秒在 html 网页上的选项卡之间移动

转载 作者:行者123 更新时间:2023-11-28 00:52:05 24 4
gpt4 key购买 nike

我从 Detecting idle time in JavaScript elegantly 得到了下面代码的基础

但它并没有真正起作用,除非我在浏览网页后有鼠标移动。如果我想通过单击菜单栏加载我的下一个 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);
}

}

关于javascript - 如果用户空闲,我如何编写一个 javascript 函数每隔几秒在 html 网页上的选项卡之间移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46789842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com