# Multiple setTimeout/clearTimeout Within Same Function Node

**URL:** <https://discourse.nodered.org/t/multiple-settimeout-cleartimeout-within-same-function-node/25300>\
**Category:** General\
**Created:** [22 April 2020 12:10 UTC](https://discourse.nodered.org/t/multiple-settimeout-cleartimeout-within-same-function-node/25300 "2020-04-22T12:10:06Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [22 April 2020 12:15 UTC](https://discourse.nodered.org/t/multiple-settimeout-cleartimeout-within-same-function-node/25300/2 "2020-04-22T12:15:22Z")

</div>

Not dense, it isn't obvious if you've not done it before.

The trick is to remember that a set timer returns a timer object reference. So you have to assign to a variable. Then you can track/clear multiple timers.

Still looking at your flow yet but that is the basics.

From your flow, you are not saving the timers to context so they will be created/lost on each incoming msg.

* * *

Since I had a few minutes and the excercise was good for me, here is a working example - note that you need to change your triggers to use `boolean` inputs rather than text.

```auto

function timer1 () {
    // This runs when the timer expires
    // msg is set to the incoming msg at the time the timer was created
    node.send(msg)
    node.warn('TIMER IS SELF CANCELLING FOR TOPIC ' + msg.topic )
    // Delete the entry from the context var and write back
    delete timers[msg.topic]
    context.set('timers', timers)
}

// Get the timers from the context store
let timers = context.get('timers') || {}

// Is there an existing timer for this topic?
if ( timers[msg.topic] ) {
    // If so, is the NEW payload TRUE?
    if ( msg.payload === true ) {
        // Cancel the timer & delete from context
        node.warn('CANCELLING TIMER FOR EXISTING TOPIC ' + msg.topic )
        clearTimeout(timers[msg.topic])
        delete timers[msg.topic]
    } else {
        // NEW payload is FALSE so create a timer
        // NB: timouts cancel themselves when they run
        node.warn('CREATING NEW TIMER FOR EXISTING TOPIC ' + msg.topic )
        timers[msg.topic] = setTimeout(timer1, 5000)
    }
} else {
    // Only interested in a payload of FALSE since 
    // there will be no timer to cancel in this case
    if ( msg.payload === false ) {
        // NEW payload is FALSE so create a timer
        // NB: timouts cancel themselves when they run
        node.warn('CREATING NEW TIMER FOR NEW TOPIC ' + msg.topic )
        timers[msg.topic] = setTimeout(timer1, 5000)
    }
}

// Update the timers to the context store
context.set('timers', timers)

```

You could refactor that code slightly to simplify the nested if's.

I also suspect that it would be possible to get race conditions with this code if you were sending through topics and cancelling them faster than the timeout value.

---

_[View the full topic](https://discourse.nodered.org/t/multiple-settimeout-cleartimeout-within-same-function-node/25300)._
