Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to change the value of circle.top?

I have made a circle with CSS, selected it in my circle variable, and used getBoundingClientRect() on it. I also have a fall() function that takes a rate and adds that rate to circle.top. Then I setInterval() to run the fall() function every half second. However, the circle.top does not seem to change. I am new to this method, so what am I missing?

<html>
<head>
  <style>
  .circle {
      padding: 15px;
      background-color: black;
      border-radius: 50%;
      width: 0.2%;
    }
  </style>
  </head>
  <body>
    <div class = circle></div>
  <script>
    let circle = document.getElementsByTagName('div')[0].getBoundingClientRect();

    function fall(rate) {

      circle.top += rate;
    }

    setInterval(fall(1), 500);
    </script>
  </body>
</html>

1 Answer

setInterval takes a function that takes no arguments. fall takes one argument. And fall(1) isn't a function, because JavaScript first executes fall(1), and it returns nothing, and the nothing (also called null) is passed into setInterval, which causes an error. Try to do something like this:

setInterval(function () {
    fall(1);
}, 500);

// Or, with the arrow function syntax:

setInterval(() => fall(1), 500);

Hmm... It still seems to be doing the same thing. Here is the updated code:

<html>
<head>
  <style>
  .circle {
      padding: 15px;
      background-color: black;
      border-radius: 50%;
      width: 0.2%;
    }
  </style>
  </head>
  <body>
    <div class = circle></div>
  <script>
    let circle = document.getElementsByTagName('div')[0].getBoundingClientRect();

    function fall(rate) {

      circle.top += rate;
    }

    setInterval(function() {fall(1)}, 500);
    </script>
  </body>
</html>

I have updated it to use the CSS top property and called the fall() function in another function. Now it works but it only does it once instead of every 500 milliseconds

<html>
<head>
  <style>
  .circle {
      position: relative;
      padding: 15px;
      background-color: black;
      border-radius: 50%;
      width: 0.2%;
    }
  </style>
  </head>
  <body>
    <div class = circle></div>
  <script>
    let circle = document.getElementsByTagName('div')[0];

    function fall(rate) {

      circle.style.top += rate;
    }

    function execute() {
      fall(2);
    }

   setInterval(execute, 500);
    </script>
  </body>
</html>