# Where to store handle of setInterval?

**URL:** https://discourse.nodered.org/t/where-to-store-handle-of-setinterval/54028
**Category:** Developing Nodes
**Created:** [20 November 2021 11:20 UTC](https://discourse.nodered.org/t/where-to-store-handle-of-setinterval/54028 "2021-11-20T11:20:15Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![danube](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danube/32/34348_2.png) [@danube](https://discourse.nodered.org/u/danube)
#### Post date: [20 November 2021 11:20 UTC](https://discourse.nodered.org/t/where-to-store-handle-of-setinterval/54028/1 "2021-11-20T11:20:15Z")

</div>

A node which I develop needs to be run periodically. With setInterval, I could achieve this. It is necessary that the interval is set up upon loading the node. Here's my simple solution:

```auto
function ShadingNode(originalConfig) {
    RED.nodes.createNode(this,originalConfig);
        const loopIntervalTime = 5000;
        function mainloopFunc(){ ... }
        setInterval(mainloopFunc, loopIntervalTime)
        this.on('input', function(msg,send,done) {
            ...

```

So far, that works already. But I realized, always when I deploy anything, an additional interval starts. So the idea is to clearInterval first. But where to store the handle? I decided to use the node context.

```auto
        if (context.loopIntervalHandle) {
            clearInterval(context.loopIntervalHandle);
            context.loopIntervalHandle = null;
        };
        context.loopIntervalHandle = setInterval(mainloopFunc, loopIntervalTime)

```

This basically works fine, but after loop number six, I receive the following warning on the console:

```auto
[warn] Context 74363dbc26183750:43e75d.3e2038a4 contains a circular reference that cannot be persisted

```

Though I' curious why this warning does not appear immediately at start, I more am curious about some kind of best practice. Where should I store the handle? Maybe there's a more practicable solution.

Thanks! Tom

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [20 November 2021 12:10 UTC](https://discourse.nodered.org/t/where-to-store-handle-of-setinterval/54028/2 "2021-11-20T12:10:24Z")

</div>

There's no need to use context. You can just define at as a variable inside the node constructor and it will be in scope and available to use.

---

<div class="post-metadata">

### Author: ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)
#### Post date: [20 November 2021 12:52 UTC](https://discourse.nodered.org/t/where-to-store-handle-of-setinterval/54028/3 "2021-11-20T12:52:27Z")

</div>

> [@danube](#):
>
> always when I deploy anything, an additional interval starts

@danube,  
Also you might call clearInterval in the node's "close" event...

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [20 November 2021 16:09 UTC](https://discourse.nodered.org/t/where-to-store-handle-of-setinterval/54028/4 "2021-11-20T16:09:15Z")

</div>

This is close to the limit of what I know how to do with JS and NR, so I hope I won't mislead you -- and I hope that someone will correct me if I do.

> [@danube](#):
>
> always when I deploy anything, an additional interval starts.

Do you mean this happens when you edit your flow and re-deploy? I would expect that to occur if you do a full deploy or restart. Deploying only modified nodes or (possibly) modified flows should prevent it. In any event, as @BartButenaers says, a `clearInterval` in the `on('close',...)` function should eliminate the issue. In general, anything your node does asynchronously should be checked for possible cleanup when the node closes.

> [@danube](#):
>
> after loop number six, I receive the following warning

By any chance, are you storing context in the `localfilesystem`? Six intervals of five seconds each corresponds exactly to the 30-second default `flushInterval` for writing to "disk." If you are storing something that does not serialize properly it could account for the warning message.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [20 November 2021 20:11 UTC](https://discourse.nodered.org/t/where-to-store-handle-of-setinterval/54028/5 "2021-11-20T20:11:09Z")

</div>

As Nick said you don't need to use context for this... I usually do something like `node.myInterval = setInterval(.... ` to hold the reference - and then in the on close you can do... . `if (node.myInterval) { clearInterval(node.myInterval) }` to tidy it up.

---

<div class="post-metadata">

### Author: ![danube](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danube/32/34348_2.png) [@danube](https://discourse.nodered.org/u/danube)
#### Post date: [23 November 2021 14:48 UTC](https://discourse.nodered.org/t/where-to-store-handle-of-setinterval/54028/6 "2021-11-23T14:48:31Z")

</div>

@knolleary @dceejay I was already afraid there would be a simple solution I just wasn't able to figure out. Using the constructor seems to be that kind of working way.

@drmibell Your assumption about localfilesystem is correct and explains the event after six times five loops.

@BartButenaers @drmibell @dceejay Haven't had an on close listener but now I do have one which clears the handle, in addition to a clearInterval one line above the setInterval call.

Thans all of you for leading me straight forward! Tom

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [22 January 2022 14:49 UTC](https://discourse.nodered.org/t/where-to-store-handle-of-setinterval/54028/7 "2022-01-22T14:49:00Z")

</div>

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