Javascript点击按钮,如果不活动60秒

javascript click button if inactive for 60 seconds

本文关键字:如果不 不活动 60秒 如果 按钮 Javascript      更新时间:2023-09-26

我已经搜索了很长时间,制作了不同的文件,这些文件不能正常工作。

我需要什么:如果我不与网页交互1分钟执行以下操作:-点击按钮。- 30秒后如果仍然没有交互,重复点击

当我回到网页(再次交互),我不希望这个脚本继续运行…只有当我不再和你交往的时候。

I've come to this:

function findButtonbyTextContent(text) {
var buttons = document.getElementsByTagName('button');
for (var i=0; i<buttons.length; i++) {
if (buttons[i].firstChild.nodeValue == text)
  return buttons[i];
}
}
function refresh(tijd){
setInterval(findButtonbyTextContent("Refresh").click(), tijd);
}
document.onmousemove = (function() {
var onmousestop = function() {
        refresh(30000);
    }, thread;
return function() {
    clearTimeout(thread);
    thread = setTimeout(onmousestop, 60000);
};
})();

我是这样做的

var int, timer, btn = document.getElementById('button');
document.onmousemove = function () {
    clearTimeout(timer);
    clearInterval(int);
    timer = setTimeout(function() {
        int = setInterval(function() {
            btn.click();
        }, 30000);
    }, 60000);
}

小提琴

我给按钮一个ID并缩短了在Fiddle中的时间,以便不必等待几分钟才能看到结果。

作为旁注,你的代码有一些问题,例如这个
setInterval(findButtonbyTextContent("Refresh").click(), tijd);

立即调用click函数并返回undefined,所以它根本不做你认为它做的事情,并且你没有在任何地方存储对间隔的引用,只存储到初始setTimeout,清除它不会清除间隔,它仍然继续。