Nuñito Calzada
Nuñito Calzada

Reputation: 2056

Calling a function every X seconds not working

I have a thymeleaf template with this piece of code:

$(document).ready(function() {
  (function() {
    alert('lalala');
    // do some stuff
    setTimeout(arguments.callee, 1000);
  })();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

I am expecting to see al alert in the page every second,] but nothing happens and I don't see any error in the console

Upvotes: 0

Views: 88

Answers (1)

Jeff Huijsmans
Jeff Huijsmans

Reputation: 1418

arguments.callee is deprecated, if I recall correctly.

If you want to execute a piece of code every x milliseconds, this might suit you. Using setInterval instead of setTimeout.

function repeatMe() {
  console.log("Lalala");
  //alert("Lalala");
}
var interval = setInterval(repeatMe, 1000);
repeatMe(); // To start it immediately

// Use this to stop the interval:
// clearInterval(interval);

If you really need to use setTimeout instead of setInterval, just call setTimeout again from the repeatMe function.

Upvotes: 2

Related Questions