# Intervals and Function

**URL:** <https://discourse.nodered.org/t/intervals-and-function/49091>\
**Category:** General\
**Created:** [29 July 2021 17:56 UTC](https://discourse.nodered.org/t/intervals-and-function/49091 "2021-07-29T17:56:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvinchira62](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mvinchira62](https://discourse.nodered.org/u/mvinchira62)\
**Post date:** [29 July 2021 17:56 UTC](https://discourse.nodered.org/t/intervals-and-function/49091/1 "2021-07-29T17:56:19Z")

</div>

I need help to turn on a buzzer at different intervals according to the variation in the input, I am currently simulating with a slider, but at the time of varying the interval is not deactivated, I attach the flow and function block

```auto
var msgHigh = {payload : 1};
var msgLow = {payload : 0};
var dato = {payload : 0};
var dato2 = {payload : 0};
function high(){
        node.send(msgHigh);
    }
function low(){
        setTimeout(function(){
            node.send(msgLow);
        },1000);
}
if(msg.payload=="0"){
        clearInterval(dato);
        clearInterval(dato2);
}else if(msg.payload=="1"){
        dato = {payload : setInterval(high,2000)};
        dato2 = {payload : setInterval(low,2000)};  
}else if(msg.payload=="2"){
        clearInterval(dato);
        clearInterval(dato2);
}else if(msg.payload=="3"){ 
        clearInterval(dato);
        clearInterval(dato2);
}

```

![WhatsApp Image 2021-07-29 at 12.53.38](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/3/934599675a2847f0cfb9d569151b89a82bee3972.jpeg)

---

<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:** [29 July 2021 17:58 UTC](https://discourse.nodered.org/t/intervals-and-function/49091/2 "2021-07-29T17:58:22Z")

</div>

You need to save the timeout to a context variable and recover it each time the function node is triggered by an incoming msg. By saving a reference, you can cancel and recreate it easily.

---

<div class="post-metadata">

**Author:** ![mvinchira62](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mvinchira62](https://discourse.nodered.org/u/mvinchira62)\
**Post date:** [29 July 2021 19:03 UTC](https://discourse.nodered.org/t/intervals-and-function/49091/3 "2021-07-29T19:03:13Z")

</div>

Could you show me how?

---

<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:** [29 July 2021 20:11 UTC](https://discourse.nodered.org/t/intervals-and-function/49091/4 "2021-07-29T20:11:09Z")

</div>

Hmm, my live example uses a custom node.js module which would be hard to unpick.

Do you know how to get and set a context variable in a function node? If not start with the node-red docs and learn how to do that first.

When you have that, create a simple setInterval to output some text to the debug panel. and then add an if statement so that if the msg.payload = "kill", cancel the interval. Maybe something like this (untested):

```auto
let myInterval = context.get('myInterval') || null

if ( msg.payload === 'kill' ) {
   clearInterval(myInterval)
} else if ( msg.payload === 'create' ) {
   myInterval = setInterval( function() {
      node.warn('PING')
   }, 5000)
}

context.set('myInterval', myInterval)

```

---

<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:** [27 September 2021 20:11 UTC](https://discourse.nodered.org/t/intervals-and-function/49091/5 "2021-09-27T20:11:24Z")

</div>

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