this code takes some values from a page, compares them and presses a button if true, and it does that in a loop
Running this pice of javascript in chrome console works fine for some time and then it runs two time. The script works fine with a compiler so why does it sometimes do that in Chrome console? And is there a way to execute the code just once all the time, without taking out setInterval
or setTimeout
or if, as I absolutely need them!
setInterval(function() { var time = document.getElementById("timer").innerHTML;; if(time>0){ setTimeout(function() { var x2b = document.getElementById("2x").innerHTML; var x3b = document.getElementById("3x").innerHTML; var evt = document.createEvent("MouseEvents"); if (x3b< x2b) { evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); var run = document.getElementById("3x"); run.dispatchEvent(evt);} }, time);} }, 2000);
Probably a problem with your code and not Chrome.
How does #timer
get updated? If it's value happens to be something like 2500
then the setTimeout
will be placed on the call stack. Then 2000ms later the next interval will occur. If #timer
happens to be something like 500
at that time then another setTimeout
will be placed on the call stack. After another 500ms both of the timeouts will be triggered, one right after another. This may make it appear that the timeout is occurring twice.
It would be helpful to record the current time ( Date.now()
) and what the value of #timer
is, and log that info when the timeout occurs.
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.