Lor-Saba / Code-Injector

WebExtension
GNU General Public License v3.0
186 stars 41 forks source link

setInterval only runs for 5-6 miliseconds #13

Open non-bin opened 4 years ago

non-bin commented 4 years ago

Injecting the script

setInterval(console.log('hi'), 1);

only runs approximately 5 times (5 milliseconds) then stops. re-injecting the script has the same effect, but slows down the browser with each injection until the tab is closed.

Filius-Patris commented 3 years ago

You could try setInterval(function() {console.log('hi');}, 1);.

The setInterval() function expects a function as the first argument (MDN Reference). When the JS engine sees your code, it evaluates the console.log('hi') expression hoping to get a function. This expression has the side effect of printing 'hi'. Thus it only gets printed once.

You have a delay of 5ms (not 1ms) because of the way concurrency works in JS. Talking about the event loop is off topic, but just keep in mind that you can't have exact sleep timings in JS.