Question about certain code

How the heck is the code being displayed in the p tag? There is nowhere where the function is invoked. And it is not self invoking.

<!-- Display the countdown timer in an element -->
<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get today's date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

  // If the count down is finished, write some text
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
</script>

Hello!

Well, it’s kind of self invoked:

So, in your code, the anonymous function is run forever until the interval is cleared, here:

if (distance < 0) {
    clearInterval(x); // Here is where it stops
    document.getElementById("demo").innerHTML = "EXPIRED";
}

And it’s self invoked because once the page is loaded, the setInterval function is called and it executes the rest.

Where is it called?

Why create a “var x” when you are not even going to call it?

The reply button doesnt work well at all but I asked another question above,

Thanks

Here:

var x = setInterval(function() { // <-- Here is where the execution starts

  // Get today's date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

  // If the count down is finished, write some text
  if (distance < 0) {
    clearInterval(x); // Here you're using the 'x' variable
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);

setInterval is, itself, calling the function that gets passed as the first parameter. var x = setInterval gets invoked by the browser when it evaluates the <script></script>.

You know you can pass functions as parameters in JavaScript? (If you don’t, I can explain that to you).

1 Like

The function clearInterval has to to take a reference to the actual interval that it’s supposed to be clearing, otherwise there isn’t a way to stop it repeating.

1 Like

Yea I guess I was just confused why make a variable x if you are not going to use it

Yea I guess I was just confused why make a variable x if you are not going to use it…

But do you understand it now?

Whenever you don’t understand some JavaScript code, copy/paste it here: http://www.pythontutor.com/javascript.html#mode=edit. This will show you what the code does.

3 Likes