What is the maximum millisecond interval that can be used in the Javascript setInterval?

Asked

Viewed 188 times

4

I would like to know the maximum range that can be used in the method setInterval javascript.

I ask this, because I used setInterval the way below and it did not execute. I don’t know if it’s something in the code or some time limit that setInterval has.

//Aqui repitimos a função de cima a cada 60 segundos 
let logar_usuario = setInterval(logarUsuario, 60000);


function logarUsuario() {
  $.ajax({
    type: "POST",
    url: "../banco/atualizar-log-usuario/deixar-usuario-logado.php",
    cache: false

  }).done(function() {


  }).fail(function() {


  }).always(function(data) {


  });
}

  • In this case, I would like to call the function every 60 seconds. I don’t know if setInterval() supports this full time interval.
  • 1

    At first there is no limit (at least I do not know). 60 seconds is very little, and certainly supports this value. If your code doesn’t work, the problem must be another.

  • 1

    I also executed the snippet above and waited 60 seconds and came reply normally. As I said, the problem may be another. Decrease the time to 10 seconds and see if anything pops up on the console after 10 seconds.

1 answer

9


As we can see in documentation of that method, there is, yes, a limit to the argument passed to him:

Maximum delay value (Maximum delay value)

The argument is converted to a 32-bit Signed integer (which supports an interval ranging from a negative to a positive number). This limits the argument to 2147483647 milliseconds.

Therefore, the largest positive number that can be passed to the method is 2147483647, which is much larger than the number you’ve spent (60000). Thus, the problem is probably elsewhere in the code.

  • 2

    Cool! I hadn’t seen this information. Just out of curiosity, this value would be over 596 thousand hours and over 68 years :D

  • 1

    Ball show, emanoellucas. Thank you!

  • And from what I read in the documentation, it follows the same principle for setTimeout().

  • 1

    @Sam that would be in seconds, it’s a thousand times less, but it’s still almost a month...

  • There goes a little guy and downvote the question. With no reason to. That’s faltering.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.