Repeat When Done with a Repeat Operator

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

Apps often have scenarios where one event controls another. In operators, this requires passing one broadcaster in and using it to control another broadcaster. You can think, "when this broadcaster fires, do that with the other broadcaster". This lesson covers using one broadcaster to control when another broadcaster repeats.

Instructor: [0:00] Rather than automatically repeating, it's important to be able to repeat when another broadcaster fires. Let's make a copy of repeat and name this one repeatWhen. We'll add in a whenBroadcaster. The scenario I want is if i for of, let's make sure we can import that. i for of on the word cat, so I'll create this as a word broadcaster.

[0:25] I want to be able to repeat when I click on the input box. If I set up a input click and use my addListener, we'll grab the input and onClick. If I say, "repeat when input click" on the word and I want to log this out, right now if I hit save, you'll see cat firing over and over again.

[0:56] Let's comment that out and hit save, because for of keeps firing off done, which keeps setting up the new broadcaster in that recursive loop. We want to wait here until whenBroadcaster fires and just ignore that value. Then we want to set up the new broadcaster.

[1:13] This should work right away. If I hit save here, you'll see cat. If I click, you'll see cat again. Click, see cat again. We have the initial, the second, and the third. One thing to notice is that on this input I now have three listeners. We also need to set up a cancel when and define that. Then if that exists, make sure to cancel when, if that's defined.

[1:46] Now, I'll click one, two times. If I refresh on my event listeners, I still only have that one event listener. If I check my console, you'll see the three cats in there. We need to bring this down to here as well. We'll create a function and paste that there. We have a fully working cancelable repeater.

[2:08] The cool part with the event listeners here, I check my elements, you'll see in this scenario the event listener shows up right away. If instead of the word, we did a create timeout, we'll set that to one second. I'll hit save. You see the event listener doesn't show up right at the beginning. I have to refresh. Then it would show up after that one second.

[2:28] This doesn't automatically refresh unless you click that. You'll see in my console I'll get that null. If I click again, I'll get that null. I'll still only have that one listener. That listener doesn't show up. I'm not able to repeat until I click on that box.

egghead
egghead
~ 23 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today