Adding a Delay?

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.

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.


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.

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.


1 Like

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.


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


:grinning: 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.

1 Like

I’m using the 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

@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.

Would that work?


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]( for Windows 10

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!

1 Like

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

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);

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

    //insert code here

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