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);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744144321a4560350.html
评论列表(0条)