简体   繁体   中英

Kill refresh script onclick

I'm trying to write a very simple auto-refreshing userscript that immediately stops refreshing when the page is clicked anywhere. I have absolutely no coding experience so help would be greatly appreciated.

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();
    };
})();

(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();
};

})();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM