Code-Injector icon indicating copy to clipboard operation
Code-Injector copied to clipboard

setInterval only runs for 5-6 miliseconds

Open non-bin opened this issue 4 years ago • 1 comments

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.

non-bin avatar Jan 24 '20 04:01 non-bin

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.

Filius-Patris avatar Nov 29 '20 00:11 Filius-Patris