javascript - Delay function with greasemonkey - Stack Overflow

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

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

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);
    }
};
Share Improve this question edited Nov 27, 2011 at 20:08 Riccardo asked Nov 27, 2011 at 16:46 RiccardoRiccardo 411 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You can simply track state:

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

I'd also remend 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 plete
function CheckForZero() {
    ... snip ...
    setTimeout(CheckForZero, 30000);
}

// no need for setInterval because it makes things heavier

var d = new Date();
var seconds = d.getSeconds()
var milliseconds = d.getMilliseconds()
var msLeft = 60 * 1000 - seconds * 1000 - milliseconds;
unsafeWindow.setTimeout(doSomething,msLeft)

function doSomething(){
   // your work here

   unsafeWindow.setTimeout(doSomething,30000);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744144321a4560350.html

相关推荐

  • javascript - Delay function with greasemonkey - Stack Overflow

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

    8天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信