# How do you clear an interval inside a callback function?

**URL:** <https://forum.8thwall.com/t/how-do-you-clear-an-interval-inside-a-callback-function/2811>\
**Category:** Technical Support\
**Tags:** FAQ, general\
**Created:** [September 17, 2024, 10:35am UTC](https://forum.8thwall.com/t/how-do-you-clear-an-interval-inside-a-callback-function/2811 "2024-09-17T10:35:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![amitri](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@amitri](https://forum.8thwall.com/u/amitri)\
**Post date:** [September 17, 2024, 10:35am UTC](https://forum.8thwall.com/t/how-do-you-clear-an-interval-inside-a-callback-function/2811/1 "2024-09-17T10:35:19Z")

</div>

Here is an example from my code run at Add function

```auto
const handlePlayFoam = (e) => {
      // Show foam effect by set opacity = 1
      ecs.Material.set(world, schemaAttribute.cursor(eid).foamEffectGo,
        {textureSrc: array[dataAttribute.cursor(eid).frame], opacity: 1})

      const intervalId = world.time.setInterval(() => {
        if (!dataAttribute.cursor(eid).isStop) {
          if (dataAttribute.cursor(eid).frame % 2 === 0 && Math.random() >= 0.43) {
            ecs.Material.set(world, schemaAttribute.cursor(eid).foamEffectGo,
              {textureSrc: array[dataAttribute.cursor(eid).frame], opacity: 0})
          } else {
            ecs.Material.set(world, schemaAttribute.cursor(eid).foamEffectGo,
              {textureSrc: array[dataAttribute.cursor(eid).frame], opacity: 0.9})
            dataAttribute.cursor(eid).frame++
          }

          if (dataAttribute.cursor(eid).frame > array.length - 1) {
            dataAttribute.cursor(eid).frame = 0
          }
        }
      }, 14)
    }

    world.events.addListener(world.events.globalId, 'RemoveCountDown', handlePlayFoam)

```

When the **handlePlayFoam** callback function runs, a loop will start inside this callback. It is assigned to **intervalId**. However, I cannot use world.time.clearInterval(intervalId) to clear it. How can I stop it completely?

---

<div class="post-metadata">

**Author:** ![Evan](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/evan/32/431_2.png) [@Evan](https://forum.8thwall.com/u/Evan)\
**Post date:** [September 18, 2024, 8:44pm UTC](https://forum.8thwall.com/t/how-do-you-clear-an-interval-inside-a-callback-function/2811/2 "2024-09-18T20:44:17Z")

</div>

Where in your code are you trying to call `clearInverval`?

Perhaps you want to declare `intervalId` outside of the callback:

```auto
let intervalId

const handlePlayFoam = () => {
  intervalId = world.time.setInterval(...)
}

handlePlayFoam()
world.time.clearInterval(intervalId)

```

---

<div class="post-metadata">

**Author:** ![amitri](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@amitri](https://forum.8thwall.com/u/amitri)\
**Post date:** [September 20, 2024, 5:28am UTC](https://forum.8thwall.com/t/how-do-you-clear-an-interval-inside-a-callback-function/2811/3 "2024-09-20T05:28:48Z")

</div>

Thanks for your support!

---

<div class="post-metadata">

**Author:** ![Phuc\_Thai](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.8thwall.com/phuc_thai/32/1062_2.png) [@Phuc\_Thai](https://forum.8thwall.com/u/Phuc_Thai)\
**Post date:** [September 20, 2024, 7:39am UTC](https://forum.8thwall.com/t/how-do-you-clear-an-interval-inside-a-callback-function/2811/4 "2024-09-20T07:39:50Z")

</div>

I have the same problem. I tried your code, but it reported the error

_\> TypeError: world.time.clearInterval is not a function_

I think it should run in the local context of that entity. Therefore, I tried removing “world.time.” and it ran smoothly 🤩. Here is my code:

```auto
let intervalId;
const handlePlayFoam = () => {
  intervalId = setInterval(...);
}

const removeThis = () => {
  clearInterval(intervalId); 
  world.deleteEntity(eid);
}

world.events.addListener(world.events.globalId, 'RemoveCountDown', handlePlayFoam);
world.events.addListener(world.events.globalId, 'GameOverEvent', removeThis);

```

---

<div class="post-metadata">

**Author:** ![amitri](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@amitri](https://forum.8thwall.com/u/amitri)\
**Post date:** [September 20, 2024, 7:47am UTC](https://forum.8thwall.com/t/how-do-you-clear-an-interval-inside-a-callback-function/2811/5 "2024-09-20T07:47:41Z")

</div>

Thanks for your support. But I have a question about how to remove an interval when it’s running global with add “_world.time._” before it.  
For example:

```auto
intervalId = world.time.setInterval(...)

```

**How do I remove it?**

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/x8thwall/original/2X/9/9212a857458f1b03a6accb129e0f8cbea408e3e5.png) [@system](https://forum.8thwall.com/u/system)\
**Post date:** [September 24, 2024, 7:48am UTC](https://forum.8thwall.com/t/how-do-you-clear-an-interval-inside-a-callback-function/2811/6 "2024-09-24T07:48:33Z")

</div>

Closed on September 24, 2024, 7:48am UTC

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.
