admin管理员组文章数量:1396871
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
2 Answers
Reset to default 5You 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);
}
本文标签: javascriptDelay function with greasemonkeyStack Overflow
版权声明:本文标题:javascript - Delay function with greasemonkey - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744144320a2592764.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论