6

I'm trying to create an automated program by using javascript console only. I need to use setInterval to make a loop for my program. The relevant part of the code is just like this:

refreshIntervalId=setInterval(tick,500);

So the "tick" function will be recalled every 500 milliseconds.

But the problem is whenever the browser tab is being inactive i.e I minimize it to do something else, the setInterval stop working. When I open the browser again it suddenly repeated so many times to "compensate" for the time the fucntion had not been executed. It is not what I am expected.

I've heard that web workers can solve the problem. I just need one (or two) single code line to work via web workers but since I'm an amateur to IT, I cannot understand all of them. I can only access the browser via JS console and I don't have the access to both HTML and its scripts.

My main goal is to make it loop and break it (setInterval and clearInterval) normally while the browser can still in the inactive mode.

What should I do now? Thank you for reading and hopefully someone can help me solve it!

Vũ Nguyễn
  • 121
  • 2
  • 5
  • 1
    Why do you think it's either necessary or appropriate for your code to be running when that tab is not open? – T.J. Crowder Jul 14 '16 at 12:15
  • See if this can help you http://stackoverflow.com/questions/5927284/how-can-i-make-setinterval-also-work-when-a-tab-is-inactive-in-chrome – Erick Gallani Jul 14 '16 at 12:23
  • *"I can only access the browser via JS console and I don't have the access to both HTML and its scripts."* That doesn't make much sense. – T.J. Crowder Jul 14 '16 at 12:40
  • Hi Crowder I have to make an automated program for clicking buttons on a website, even when I was typing a document or doing something else. What I mean by "access the browser via JS console" is that I can only write in Javascript console to make the program, I did not have access to the website HTML and its scripts so Web workers is a little bit hard. – Vũ Nguyễn Jul 14 '16 at 13:05
  • Gallani: Thank you, I used the Hack Timer, it was great until the page refreshed itself. I don't know why but it would halted all the code. – Vũ Nguyễn Jul 14 '16 at 13:15

1 Answers1

9

Okay, this might be a little late, but I hope this will be of some use anyway.

Put this code into your main JavaScript:

let intervalWorker = new Worker('worker.js');
intervalWorker.onmessage = /* Your function here */;

...and this could be an example for the code in a new file 'worker.js':

setInterval(() => {
    postMessage();
}, 500);

The worker will post an empty message to your main thread every 500ms. When that happens, your main thread will call the "onmessage" function on your worker. So just assign whatever you function you need to happen every 500ms to that property.

Note: You need to run this on a server for the JS to load the worker file - otherwise it will complain because of 'safety' reasons.

DavidsKanal
  • 655
  • 11
  • 15
  • How can I have more granular control over the interval? Meaning starting it when I want to and clearing it when I want to. I'm not familiar with `Workers` and relevant documentation is slim – FrenchMajesty Oct 29 '20 at 13:49
  • setTimeout and setInterval work the same in the Window scope and in a worker scope; I suggest you just read the MDN documentation on setInterval. You can start the interval by calling setInterval, and if you safe its return value like `let id = setInterval(...)`, then you can clear it again later using `clearInterval(id)`. If you want to communicate these actions to a work, you'll need to work with postMessage. – DavidsKanal Oct 29 '20 at 17:03