Return to Video

Animating DOM with setInterval (Video Version)

  • 0:01 - 0:04
    I've got this webpage
    with a picture of Oh Noes,
  • 0:04 - 0:08
    who is freaking out that
    the world is going to end soon--
  • 0:08 - 0:10
    specifically, in 500 seconds.
  • 0:10 - 0:13
    I want to make this webpage
    way more useful,
  • 0:13 - 0:17
    by turning this number into
    a live countdown,
  • 0:17 - 0:21
    so that visitors can see exactly
    how much time they have left.
  • 0:21 - 0:24
    Now, when we animate
    part of a webpage,
  • 0:24 - 0:27
    the strategy is to find
    some element in it,
  • 0:27 - 0:29
    then change something
    about that element,
  • 0:29 - 0:33
    and do that a particular
    number of times a second.
  • 0:33 - 0:38
    So, for the first step,
    I will find the countdown by ID.
  • 0:38 - 0:39
    Simple.
  • 0:39 - 0:44
    [typing]
  • 0:48 - 0:52
    For the second step, I'll make
    a function that counts down.
  • 0:52 - 0:55
    [typing]
  • 0:55 - 0:57
    And what we'll do--
  • 0:57 - 1:01
    I want to set the `textContent`,
    and I want to set it equal to
  • 1:01 - 1:04
    the previous number minus one.
  • 1:04 - 1:07
    And the `textContent`
    will actually be a string,
  • 1:07 - 1:10
    so we want to turn it into a number.
  • 1:10 - 1:13
    We can do that using `parsefloat()`.
  • 1:13 - 1:16
    And then we can subtract one from it.
  • 1:16 - 1:22
    Okay, so finally, we want to
    call this function on an interval,
  • 1:22 - 1:25
    which means a certain
    number of times per second.
  • 1:25 - 1:30
    And we can use that using
    `window.setInterval()`.
  • 1:30 - 1:34
    And this function takes two arguments:
  • 1:34 - 1:38
    the callback function, and
    the number of milliseconds to wait
  • 1:38 - 1:41
    before that function gets
    called back again.
  • 1:41 - 1:43
    We can specify the callback function
  • 1:43 - 1:47
    just like we specify it
    for event listeners: by name.
  • 1:47 - 1:49
    And then-- it's going really
    fast right now
  • 1:49 - 1:52
    because we haven't specified
    the second argument--
  • 1:52 - 1:55
    so for that, we want it to be
    a certain number of milliseconds,
  • 1:55 - 1:59
    and we want it once per second,
    so we're going to say a thousand,
  • 1:59 - 2:02
    because there are
    a thousand milliseconds in a second.
  • 2:02 - 2:05
    There we go, now it's
    counting down one per second.
  • 2:05 - 2:10
    So you'd better learn as much as you can
    in the next 490 seconds!
  • 2:10 - 2:15
    There is one more window function that we
    sometimes use instead of `setInterval`,
  • 2:15 - 2:17
    and that's setTimeout.
  • 2:17 - 2:22
    And I'll just change it to that,
    and see if you can see the difference.
  • 2:22 - 2:24
    Have to wait a second.
  • 2:24 - 2:28
    Okay, so, now maybe you can see
    that when we use `setTimeout`,
  • 2:28 - 2:34
    the browser only calls
    the function once, not repeatedly.
  • 2:34 - 2:39
    That's not so useful for
    when we're making animations.
  • 2:39 - 2:42
    But it can be super useful in other cases,
  • 2:42 - 2:46
    like if we showed a warning banner
    to our users, and then we wanted
  • 2:46 - 2:48
    to hide it after 10 seconds.
  • 2:48 - 2:53
    So let me change this
    back to `setInterval`.
  • 2:53 - 2:57
    Now, when we're testing
    animations like this,
  • 2:57 - 3:00
    we should really see what they look like
    at all points in the animation,
  • 3:00 - 3:02
    like what happens
    when it gets down to zero.
  • 3:02 - 3:05
    Well that's going to take
    a really long time to get there,
  • 3:05 - 3:07
    and you're going to
    get really bored, so
  • 3:07 - 3:11
    we'll just change
    our beginning data to 5,
  • 3:11 - 3:13
    and watch what happens.
  • 3:13 - 3:17
    Four, three, two, one, zero...
  • 3:17 - 3:19
    negative one, negative two.
  • 3:19 - 3:22
    Okay, so now it's getting weird.
  • 3:22 - 3:26
    When the world ends, it should just go
    "Kaboom!" and stop counting.
  • 3:26 - 3:30
    So what we actually want to do,
    is stop this animation
  • 3:30 - 3:33
    once it gets to that zero point.
  • 3:33 - 3:36
    And we can do that using
    an `if` condition inside the function.
  • 3:36 - 3:42
    So, let me start by storing
    the current time in a variable
  • 3:42 - 3:45
    since we're going to use it a few times.
  • 3:45 - 3:48
    So I'll just take this,
    put it here,
  • 3:48 - 3:51
    and replace this with `currentTime`.
  • 3:51 - 3:56
    Now what I can do is
    have an `if` condition
  • 3:56 - 4:01
    that makes sure we only update the text
    if `currentTime` is greater than zero.
  • 4:01 - 4:05
    So that's the only time we want
    to actually subtract one.
  • 4:05 - 4:10
    So I need to move this inside here.
  • 4:10 - 4:15
    This works, but there is something
    really bad about this approach.
  • 4:15 - 4:19
    The browser is still calling the
    `countItDown` function once per second
  • 4:19 - 4:22
    as long as this webpage is open.
  • 4:22 - 4:25
    You shouldn't make browsers
    call functions for no reason--
  • 4:25 - 4:28
    they have lots of other
    important things to do.
  • 4:28 - 4:31
    What we really want to do
    is to tell the browser that
  • 4:31 - 4:36
    once it gets to zero, it doesn't need
    to call this function anymore at all.
  • 4:36 - 4:40
    We can do that using a new method:
    `window.clearInterval()`.
  • 4:40 - 4:47
    We can stick that in this `else` here--
    `window.clearInterval()`.
  • 4:47 - 4:52
    Now, we need to pass it an argument,
    so that it knows which interval to clear.
  • 4:52 - 4:56
    Because we might actually have
    multiple intervals on a page.
  • 4:56 - 4:59
    The way that we know
    which interval to clear
  • 4:59 - 5:03
    is to store the result
    of `setInterval` in a variable.
  • 5:03 - 5:08
    So now I've got it in a timer variable,
    I can copy and paste it into there,
  • 5:08 - 5:11
    and now it knows what to clear,
    so when it gets to zero,
  • 5:11 - 5:16
    it should stop updating, and
    it should stop calling that function.
  • 5:16 - 5:19
    For every animation that you make,
    you should think carefully about
  • 5:19 - 5:22
    what the condition
    should be for stopping it.
  • 5:22 - 5:26
    And yes, you might have some animations
    that should go on and on, my friends--
  • 5:26 - 5:29
    but they'd better be
    really sweet animations.
  • 5:29 - 5:34
    Because your browser is working every time
    it calls that callback function.
  • 5:34 - 5:38
    Now spin this off, and make the world
    actually go kaboom!
Title:
Animating DOM with setInterval (Video Version)
Description:

more » « less
Video Language:
English
Duration:
05:39

English subtitles

Revisions