# Timed Loop Question

**URL:** <https://forum.code.org/t/timed-loop-question/15062>\
**Category:** Uncategorized\
**Tags:** apcsp\
**Created:** [March 17, 2018, 3:11pm UTC](https://forum.code.org/t/timed-loop-question/15062 "2018-03-17T15:11:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkeays](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jkeays/32/5313_2.png) [@jkeays](https://forum.code.org/u/jkeays)\
**Post date:** [March 17, 2018, 3:11pm UTC](https://forum.code.org/t/timed-loop-question/15062/1 "2018-03-17T15:11:34Z")

</div>

Question:

I know it is possible to have 2 or more timed loops going on at the same time but I would like to stop one timed loop and CONTROL which one to stop. Since the loops are not given names/labels, how can I turn multiple timedLoops on and off with total control?

Thanks

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/josh/32/868_2.png) [@josh](https://forum.code.org/u/josh)\
**Post date:** [March 17, 2018, 8:20pm UTC](https://forum.code.org/t/timed-loop-question/15062/2 "2018-03-17T20:20:37Z")

</div>

When you start a timed loop, you can optionally assign it to a variable. Doing so lets you later reference each loop separately. The `stopTimedLoop()` function, if called without an argument, stops all timed loops, but if you’ve assigned your loops to variables, you can pass that variable as an argument to stop a specific loop. Eg:

```javascript
var loop1 = timedLoop(500, function() { console.log("looping") });
var loop2 = timedLoop(500, function() { console.log("still looping") });
stopTimedLoop(loop1); // Only stops loop1, loop2 keeps running

```

Here’s a quick example in App Lab [https://studio.code.org/projects/applab/CH9h52TGnJJ8L8GXjO2H9Q/view](https://studio.code.org/projects/applab/CH9h52TGnJJ8L8GXjO2H9Q/view)

---

<div class="post-metadata">

**Author:** ![jkeays](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jkeays/32/5313_2.png) [@jkeays](https://forum.code.org/u/jkeays)\
**Post date:** [March 18, 2018, 5:13am UTC](https://forum.code.org/t/timed-loop-question/15062/3 "2018-03-18T05:13:24Z")

</div>

**What a great explanation. I really appreciate it!**

I took your code and added a few things to test a few other theories.  
Enjoy

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

---

<div class="post-metadata">

**Author:** ![lightningmagna4917](https://avatars.discourse-cdn.com/v4/letter/l/71c47a/32.png) [@lightningmagna4917](https://forum.code.org/u/lightningmagna4917)\
**Post date:** [April 19, 2018, 10:56pm UTC](https://forum.code.org/t/timed-loop-question/15062/4 "2018-04-19T22:56:34Z")

</div>

@josh What do you do if you have a timed loop in one function but a stopTimedLoop() in another function, and a timed loop that you don’t want to stop? If I assign the timed loop that I want to be stopped to a variable inside one function, the stop timed loop won’t recognize that variable since it’s in another function.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/josh/32/868_2.png) [@josh](https://forum.code.org/u/josh)\
**Post date:** [April 20, 2018, 1:44am UTC](https://forum.code.org/t/timed-loop-question/15062/5 "2018-04-20T01:44:48Z")

</div>

Ah, now we’re getting into variable scope. When you create a variable inside a function, it is only accessible within that function. If you want a variable to be accessible anywhere, you need to create it at the top level of your program, which will make it “global.” Fortunately, you can create an empty variable with global scope and then just assign it a value once you’re inside the function. eg.

```javascript
var loop1;
var loop2;
function startLoops() {
    loop1 = timedLoop(500, function() { console.log("looping") });
    loop2 = timedLoop(500, function() { console.log("still looping") });
}
startLoops();
stopTimedLoop(loop1); // Only stops loop1, loop2 keeps running

```

Here’s a remix of my previous example using this concept:

> **[Code.org](https://studio.code.org/users/sign_in)**
>
> Anyone can learn computer science. Make games, apps and art with code.
