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

I have this code:

    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:

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.

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.

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.

it would be virtually imperceivable.

I used to also. I learned the hard way :slight_smile:

Thanks again. Refactoring...