1

output of this code is always getting delayed by 25-30 milliseconds. Why?

i tried same using Webworker, even then output was delayed. How to fix this issue?

I want almost perfect output for the following code.

var j = 0;
var start = Date.now();
setInterval(function() {
  j++;
  if (j % 10 == 0) {
    var end = Date.now();
    console.log(end - start);
  }

}, 50);

enter image description here

Vikramaditya
  • 5,444
  • 6
  • 34
  • 45

1 Answers1

1

Javascript intervals does not guarantee that the function is called at the exact time, just that it is called "sometime after" the amount of time specified.

This link explains a bit about window.performance timing which you might be able to use, depending on what browsers you need to support. You can look at the answers in this question for some code examples.

Community
  • 1
  • 1
Robin
  • 1,251
  • 11
  • 18