杀死刷新脚本onclick

Kill refresh script onclick

我正在尝试编写一个非常简单的自动刷新用户脚本,当在任何地方单击页面时它会立即停止刷新。我完全没有编码经验,所以非常感谢帮助。

var cancelled = false;
document.body.addEventListener("click",function(){cancelled=true;});
function goodluck() {
if (cancelled=false) {
      setTimeout(function(){ location.reload(); }, Math.floor(Math.random() * 4000));}
else if (cancelled=true) {return;}
}
(function() {
    'use strict';

    var didClick = false;
    var interval = Math.floor(Math.random() * 4000);

    function refresh() {
        var timeout = setTimeout(function() {
            if (didClick) {
                return;
            }
            location.reload();
        }, interval);

        document.addEventListener('click', function clickHandler() {
            if (!didClick) {
                didClick = true;
                clearTimeout(timeout);
            }
            document.removeEventListener('click', clickHandler);
        });
   }

    window.onload = function() {
        refresh();
    };
})();

(函数() { 'use strict';

var didClick = false;
var interval = Math.floor(Math.random() * 4000);

function refresh() {
    var timeout = setTimeout(function() {
        if (didClick) {
            return;
        }
        location.reload();
    }, interval);

    document.addEventListener('click', function clickHandler() {
        if (!didClick) {
            didClick = true;
            clearTimeout(timeout);
        }
        document.removeEventListener('click', clickHandler);
    });

}

window.onload = function() {
    refresh();
};

})();