如果用户空闲,如何编写javascript函数,使其每隔几秒钟在html网页上的选项卡之间移动
以下代码的基础是我从 但除非我在网页上时鼠标移动,否则它不会真正起作用。如果我想通过单击菜单栏加载下一个html文件,然后稍微移动鼠标,然后在3秒钟内什么也不做,则选项卡将自动更改。但是在那次改变之后,我仍然让它闲置着,它只是停留在那里。我正在尝试编写一个函数,当用户每3秒持续空闲时,它会执行一些操作。如果用户空闲,我想在选项卡之间切换。我需要一个函数或者一个使用什么方法的想法如果用户空闲,如何编写javascript函数,使其每隔几秒钟在html网页上的选项卡之间移动,javascript,html,css,Javascript,Html,Css,以下代码的基础是我从 但除非我在网页上时鼠标移动,否则它不会真正起作用。如果我想通过单击菜单栏加载下一个html文件,然后稍微移动鼠标,然后在3秒钟内什么也不做,则选项卡将自动更改。但是在那次改变之后,我仍然让它闲置着,它只是停留在那里。我正在尝试编写一个函数,当用户每3秒持续空闲时,它会执行一些操作。如果用户空闲,我想在选项卡之间切换。我需要一个函数或者一个使用什么方法的想法 function idle_time(){ var t; //document.onload = resetTime
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();
// }
}工作演示-http://jsfiddle.net/Malkeet12/6Rm9S/27/
var idleInterval=设置间隔(时间增量,3000);
$scope.myListener=function(){
clearInterval(idleInterval);
//log('idleInterval');
idleInterval=setInterval(timerIncrement,3000);//1分钟
};
函数timerIncrement(){
var arr=document.getElementById(“someFormId”).elements;
对于(var i=0;i当您更改“制表符”时,您是否正在加载一个全新的html页面?如果是,该页面是否也加载此脚本?是的,它正在加载一个全新的页面,并且具有相同的脚本?何处是idle_time()
被调用?可能发生在onload
之后,由于没有交互,resetTimer
从未触发,因此setTimeout
从未启动。这可能有助于:
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);
}
}