JavaScript gotchas

Sort of part 2 to JavaScript notes.

 

JavaScript has a minimum time resolution of 4ms. If you do a setInterval with 1ms delay – it will be bumped to 4 ms.

var x = { val: 0 };
var id = setInterval(() => { x.val++; }, 1);
setTimeout(() => { clearInterval(id); console.log("X: ", x); }, 10000);

This code will print same value of x.val whether the delay is 1 or 4 ms.

Leave a Reply

Your email address will not be published. Required fields are marked *