Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay function with greasemonkey

I need a code that when CheckForZero happens for the first time, after 30 seconds happens again and it go on every 30 seconds.

var waitForZeroInterval = setInterval (CheckForZero, 0);

function CheckForZero ()
{
    if ( (unsafeWindow.seconds == 0)  &&  (unsafeWindow.milisec == 0) )
    {
        clearInterval (waitForZeroInterval);

        var targButton  = document.getElementById ('bottone1799');
        var clickEvent  = document.createEvent ('MouseEvents');

        clickEvent.initEvent ('click', true, true);
        targButton.dispatchEvent (clickEvent);
    }
};
like image 671
Riccardo Avatar asked Nov 28 '25 00:11

Riccardo


1 Answers

You can simply track state:

var hasRun = false;
function CheckForZero () {
    ... snip ...
    if (!hasRun) {
        hasRun = true;
        setInterval(CheckForZero, 30000);
    }
 }

I'd also recommend using setTimeout() rather than setInterval()/clearInterval() (since it doesn't need to be run on a recurring basis).

Edit: I edited the above code to reflect the OP's modified requirements. I've added another version below to simplify, too.

setTimeout(CheckForZero, 0); // OR just call CheckForZero() if you don't need to defer until processing is complete
function CheckForZero() {
    ... snip ...
    setTimeout(CheckForZero, 30000);
}
like image 73
sarumont Avatar answered Nov 30 '25 16:11

sarumont



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!