# Adding a Delay?

**URL:** <https://forum.code.org/t/adding-a-delay/16460>\
**Category:** Coding and Debugging Help\
**Tags:** csd-unit-6\
**Created:** [May 30, 2018, 3:10pm UTC](https://forum.code.org/t/adding-a-delay/16460 "2018-05-30T15:10:25Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric.shultz](https://avatars.discourse-cdn.com/v4/letter/e/ce7236/32.png) [@eric.shultz](https://forum.code.org/u/eric.shultz)\
**Post date:** [May 30, 2018, 3:10pm UTC](https://forum.code.org/t/adding-a-delay/16460/1 "2018-05-30T15:10:25Z")

</div>

I have a program that plays notes and flashes the LEDs. But even though there is a duration to the notes, they all play (and the lights flash) instantaneously. I can’t seem to find any sort of delay or wait. Any help would be appreciated.

> **[Check out what I made](https://studio.code.org/projects/applab/LvEI9ZyBGYQzzXTApOBlQDwvgTlRVlsiyBqEhyWm5H8)**
>
> I wrote the code myself with Code.org

---

<div class="post-metadata">

**Author:** ![elizabeth\_admin](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@elizabeth\_admin](https://forum.code.org/u/elizabeth_admin)\
**Post date:** [May 31, 2018, 4:38pm UTC](https://forum.code.org/t/adding-a-delay/16460/2 "2018-05-31T16:38:16Z")

</div>

Hi Eric,

It sounds like you might want to use the “setTimeout” block in the “Control” drawer. It lets you delay code for a certain number of milliseconds.

[https://docs.code.org/gamelab/setTimeout/](https://docs.code.org/gamelab/setTimeout/)

Elizabeth

---

<div class="post-metadata">

**Author:** ![eric.shultz](https://avatars.discourse-cdn.com/v4/letter/e/ce7236/32.png) [@eric.shultz](https://forum.code.org/u/eric.shultz)\
**Post date:** [June 1, 2018, 4:52pm UTC](https://forum.code.org/t/adding-a-delay/16460/3 "2018-06-01T16:52:08Z")

</div>

That helped somewhat. It also made me rethink my program.

It seemed like I had to keep increasing the millisecond delay as it went along, instead of being able to put the same amount of delay for each block. I guess I’ll have to experiment with it some more.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![elizabeth\_admin](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@elizabeth\_admin](https://forum.code.org/u/elizabeth_admin)\
**Post date:** [June 1, 2018, 5:25pm UTC](https://forum.code.org/t/adding-a-delay/16460/4 "2018-06-01T17:25:05Z")

</div>

Hi Eric,

Yes, it’s not exactly a delay in that it pauses the program for a particular amount of time. It schedules the code to run after a certain number of milliseconds, but the rest of the code continues to run after it gets scheduled. You will have to schedule things for different lengths of timeout if you don’t want them to happen at the same time.

Elizabeth

---

<div class="post-metadata">

**Author:** ![ctenney](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@ctenney](https://forum.code.org/u/ctenney)\
**Post date:** [December 18, 2018, 9:09pm UTC](https://forum.code.org/t/adding-a-delay/16460/5 "2018-12-18T21:09:50Z")

</div>

Glad I found this forum, I was able to make a project with this help! Thought I should share incase the example code can help anyone else.

> **[Check out what I made](https://studio.code.org/projects/applab/aAldaQ3m0qUHgo5-Fj2JL0ueiPtCw8PflWSybM7nP1c)**
>
> I wrote the code myself with Code.org

---

<div class="post-metadata">

**Author:** ![elizabeth\_admin](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@elizabeth\_admin](https://forum.code.org/u/elizabeth_admin)\
**Post date:** [December 18, 2018, 9:35pm UTC](https://forum.code.org/t/adding-a-delay/16460/6 "2018-12-18T21:35:54Z")

</div>

That’s amazing! I just played it in the Seattle office, and everyone loved it. 🙂

Elizabeth

---

<div class="post-metadata">

**Author:** ![ctenney](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@ctenney](https://forum.code.org/u/ctenney)\
**Post date:** [December 18, 2018, 10:09pm UTC](https://forum.code.org/t/adding-a-delay/16460/7 "2018-12-18T22:09:38Z")

</div>

😀 Happy you guys enjoyed it. We did not have quite enough time to finish unit 6 this semester, so we are going to end with this project.

> **[Music and Light Show on Circuit Playground](https://docs.google.com/document/d/1NKc7gz9ySCKujUJYWxkPBvacHNM2LJ3I0Xb9kWOhCIQ/edit)**
>
> Music and Light Show on Circuit Playground Use App Lab to create a project the has the Circuit Playground play music with a light show created by you. Remember to enable the Maker Tools in App Lab. Music To create your music, you may want to search...

---

<div class="post-metadata">

**Author:** ![ericwolgie](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@ericwolgie](https://forum.code.org/u/ericwolgie)\
**Post date:** [November 2, 2019, 3:24am UTC](https://forum.code.org/t/adding-a-delay/16460/8 "2019-11-02T03:24:36Z")

</div>

I’m using the [code.org](http://code.org) curriculum for the second year, and figured I’d better get my butt in gear and get better at java script. I was a database developer for years but I’m afraid my coding is awfully rusty so my apologies in advance – since I’m relatively sure I’m doing something dense:

To wit:

I’m trying to use the “setTimeout” function to slow the motion of an object on the screen so it doesn’t just jump to that spot:

I’ve tried breaking the program into simple functions, writing it all as one simple function, changing the time parameters… nothing seems to work. The object jumps and there is no animation at all. I gotta figure I’m being a bonehead but I just can’t seem to make that function work:

[code snippet](https://studio.code.org/projects/applab/J9hNBSDV7uZRJs1w8Tlc5A-KED4NcT8rmZuG-8Hu5OU/edit)

---

<div class="post-metadata">

**Author:** ![mwood](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/mwood/32/4941_2.png) [@mwood](https://forum.code.org/u/mwood)\
**Post date:** [November 2, 2019, 5:37pm UTC](https://forum.code.org/t/adding-a-delay/16460/9 "2019-11-02T17:37:36Z")

</div>

@ericwolgie, I’m not an expert at appLab, so there may be another way to do this, but I think the setTimeout function only times out whatever you put inside it (and still runs code after it) as per @elizabeth_admin’s comment above.

However, what about using the timedLoop command. I was able to get animation with it in my [remake of your project here.](https://studio.code.org/projects/applab/kH5Ew-kWmhezXQhMdI_93onbbbeI19CZ5PqTrXBIAU8)

Would that work?

Mike

---

<div class="post-metadata">

**Author:** ![ericwolgie](https://avatars.discourse-cdn.com/v4/letter/e/e19adc/32.png) [@ericwolgie](https://forum.code.org/u/ericwolgie)\
**Post date:** [November 4, 2019, 6:53pm UTC](https://forum.code.org/t/adding-a-delay/16460/10 "2019-11-04T18:53:30Z")

</div>

Thanks for the note—

I’m trying to stick to ‘standard’ js function and I don’t see timedloop there. I tried trucking things inside the SetTimeout function but that led to more issues… I’ll keep trying  
😊

Mail]([https://go.microsoft.com/fwlink/?LinkId=550986](https://go.microsoft.com/fwlink/?LinkId=550986)) for Windows 10

---

<div class="post-metadata">

**Author:** ![loducayen](https://avatars.discourse-cdn.com/v4/letter/l/90ced4/32.png) [@loducayen](https://forum.code.org/u/loducayen)\
**Post date:** [January 23, 2020, 3:59am UTC](https://forum.code.org/t/adding-a-delay/16460/11 "2020-01-23T03:59:31Z")

</div>

Thank you so much for your post. We are on Lesson 11 in Unit 6. My students were having problem with the code as provided in the lesson. The tempo and length of notes were challenging to code accurately. The code you shared works well and the link to the music was so helpful. We’re now having a full blown music recital on Friday! I can’t wait to hear my students’s creations! Thanks again for sharing!

---

<div class="post-metadata">

**Author:** ![ctenney](https://avatars.discourse-cdn.com/v4/letter/c/db5fbb/32.png) [@ctenney](https://forum.code.org/u/ctenney)\
**Post date:** [February 11, 2020, 9:17pm UTC](https://forum.code.org/t/adding-a-delay/16460/12 "2020-02-11T21:17:00Z")

</div>

So glad things went well for you! This has been one of the favorite activities in my class for several semesters now.

---

<div class="post-metadata">

**Author:** ![kookooloopdaking](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/kookooloopdaking/32/5224_2.png) [@kookooloopdaking](https://forum.code.org/u/kookooloopdaking)\
**Post date:** [February 11, 2020, 11:11pm UTC](https://forum.code.org/t/adding-a-delay/16460/13 "2020-02-11T23:11:49Z")

</div>

Hello Eric, To make it so that it it doesn’t grow, you could have them nested within each other.

```
setTimeout(function() {
    //insert code here
    setTimeout(function() {},5);
},5);

```

or instead, you could use the `setInterval()` function.

```
setInterval(function{
    //insert code here
},5)

```

this will run once every 5 milliseconds, while the `setTimeout()` function only does it once after 5 milliseconds.
