# Context \<ref\> contains a circular reference that cannot be persisted

**URL:** <https://discourse.nodered.org/t/context-ref-contains-a-circular-reference-that-cannot-be-persisted/93349>\
**Category:** General\
**Tags:** function-node\
**Created:** [22 November 2024 10:53 UTC](https://discourse.nodered.org/t/context-ref-contains-a-circular-reference-that-cannot-be-persisted/93349 "2024-11-22T10:53:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lorccan](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@lorccan](https://discourse.nodered.org/u/lorccan)\
**Post date:** [22 November 2024 10:53 UTC](https://discourse.nodered.org/t/context-ref-contains-a-circular-reference-that-cannot-be-persisted/93349/1 "2024-11-22T10:53:33Z")

</div>

I have this code:

```auto
    const intervalId = setInterval(() => {
        node.send({ payload: "test string });
    }, interval);

```

When I check the value of intervalId using node.warn, it appears to be an integer" `intervalId: 909890`. However, when I then do this: `flow.set("repeater", intervalId);` I see this in the context browser:

```auto
Exception: TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Timeout'
    | property '_idlePrev' -> object with constructor 'TimersList'
    --- property '_idleNext' closes the circle` and later a warning appears in debug: `"Context 5c6dd81ae4059479 contains a circular reference that cannot be persisted"`

```

Is there some way that I can avoid this? For info, I am trying to start and stop a repeat dynamically. Starting does not pose a problem (at least I don't think it does), but I need to be able to retrieve the ID from setInterval to be able to stop it (or, again, I think I do).

Any ideas/insight gratefully received.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 November 2024 11:01 UTC](https://discourse.nodered.org/t/context-ref-contains-a-circular-reference-that-cannot-be-persisted/93349/2 "2024-11-22T11:01:23Z")

</div>

> [@lorccan](#):
>
> Is there some way that I can avoid this? For info, I am trying to start and stop a repeat dynamically.

You could avoid the function altogether and use a context flag that permits/denies an inject sending its msg onwards?

If you want start/stop/dynamic intervals, then something like node-red-contrib-cron-plus can be set dynamically (at runtime) by `msg` to do whatever you need.

For me, the issue with doing this in a function node is you lose the node-red advantage i.e. lose the "graphical" cues of what is going on in your flows.

---

<div class="post-metadata">

**Author:** ![lorccan](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@lorccan](https://discourse.nodered.org/u/lorccan)\
**Post date:** [22 November 2024 11:05 UTC](https://discourse.nodered.org/t/context-ref-contains-a-circular-reference-that-cannot-be-persisted/93349/3 "2024-11-22T11:05:52Z")

</div>

Thanks for the quick reply. Is there any downside (performance-wise) to repeatedly firing an inject that goes nowhere? (My interval is likely to be 5s.)

And, I agree about the graphical cues: I tend to go down JS rabbit holes in functions and then have to unravel them into more normal Node-RED flows so that I to work out what's supposed to be happening.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 November 2024 11:06 UTC](https://discourse.nodered.org/t/context-ref-contains-a-circular-reference-that-cannot-be-persisted/93349/4 "2024-11-22T11:06:55Z")

</div>

> [@lorccan](#):
>
> Is there any downside (performance-wise) to repeatedly firing a repeat that goes nowhere? (My interval is likely to be 5s.)

it would be virtually imperceivable.

> [@lorccan](#):
>
> And, I agree about the graphical cues: I tend to go down JS rabbit holes in functions and then have to unravel them into more normal Node-RED flows so that I to work out what's supposed to be happening.

I used to also. I learned the hard way 🙂

---

<div class="post-metadata">

**Author:** ![lorccan](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@lorccan](https://discourse.nodered.org/u/lorccan)\
**Post date:** [22 November 2024 11:07 UTC](https://discourse.nodered.org/t/context-ref-contains-a-circular-reference-that-cannot-be-persisted/93349/5 "2024-11-22T11:07:56Z")

</div>

Thanks again. Refactoring...

---

<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:** [6 December 2024 11:08 UTC](https://discourse.nodered.org/t/context-ref-contains-a-circular-reference-that-cannot-be-persisted/93349/6 "2024-12-06T11:08:40Z")

</div>

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