# Coding functions

**URL:** <https://discourse.nodered.org/t/coding-functions/2028>\
**Category:** General\
**Created:** [3 August 2018 11:43 UTC](https://discourse.nodered.org/t/coding-functions/2028 "2018-08-03T11:43:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaffikanne](https://avatars.discourse-cdn.com/v4/letter/k/df788c/32.png) [@kaffikanne](https://discourse.nodered.org/u/kaffikanne)\
**Post date:** [3 August 2018 11:43 UTC](https://discourse.nodered.org/t/coding-functions/2028/1 "2018-08-03T11:43:20Z")

</div>

Hi  
i am trying to get at timer function to work, i have tried something like this:

var a = msg.payload;  
var myVar;

if ( a === true){  
myFunction();  
}

function myFunction() {  
myVar = setTimeout(alertFunc, 3000);  
}

function alertFunc() {  
return msg;  
}

this will not return the payload. any ideas ?  
best regards

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [3 August 2018 11:59 UTC](https://discourse.nodered.org/t/coding-functions/2028/2 "2018-08-03T11:59:56Z")

</div>

Why not use Node-RED nodes to make things simple ?

 ![r-001](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6794601acbbd558ce169ce978c41748f6d118a56.PNG)

Flow:

```auto
[{"id":"3d109f83.9e4bd","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"23fb1e3b.2faaa2","type":"inject","z":"3d109f83.9e4bd","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":300,"wires":[["b7c54366.523e8"]]},{"id":"4c0fee22.1eff1","type":"debug","z":"3d109f83.9e4bd","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":300,"wires":[]},{"id":"368eb1aa.e7518e","type":"inject","z":"3d109f83.9e4bd","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":360,"wires":[["b7c54366.523e8"]]},{"id":"b7c54366.523e8","type":"switch","z":"3d109f83.9e4bd","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":320,"wires":[["b37afd32.e166c"],["13be90fe.f9c38f"]]},{"id":"b37afd32.e166c","type":"delay","z":"3d109f83.9e4bd","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":520,"y":300,"wires":[["4c0fee22.1eff1"]]},{"id":"13be90fe.f9c38f","type":"debug","z":"3d109f83.9e4bd","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":570,"y":380,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![kaffikanne](https://avatars.discourse-cdn.com/v4/letter/k/df788c/32.png) [@kaffikanne](https://discourse.nodered.org/u/kaffikanne)\
**Post date:** [3 August 2018 12:04 UTC](https://discourse.nodered.org/t/coding-functions/2028/3 "2018-08-03T12:04:51Z")

</div>

What im trying to do in the long run is to reset a bool to false, this is for opening a door, and i only want the door to be open 15 seconds before releasing.  
Im going to use the alexa local, and need to reset the input from alexa to false after the time has passed.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [3 August 2018 12:10 UTC](https://discourse.nodered.org/t/coding-functions/2028/4 "2018-08-03T12:10:18Z")

</div>

I see. In such case it is even simpler if you use the trigger node.

 ![r-001](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bcab4a34e368f2bdc01ef79ffc968da4a90aa346.PNG)

Flow:

```auto
[{"id":"9ef6cff7.fb755","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"a2fb45e5.2aab88","type":"inject","z":"9ef6cff7.fb755","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":180,"wires":[["635b95c3.89e57c"]]},{"id":"635b95c3.89e57c","type":"trigger","z":"9ef6cff7.fb755","op1":"true","op2":"false","op1type":"bool","op2type":"bool","duration":"5","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":400,"y":180,"wires":[["c198a401.a82c78"]]},{"id":"c198a401.a82c78","type":"debug","z":"9ef6cff7.fb755","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":180,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 August 2018 12:40 UTC](https://discourse.nodered.org/t/coding-functions/2028/5 "2018-08-03T12:40:54Z")

</div>

> [@kaffikanne](#):
>
> function alertFunc() {  
> return msg;  
> }

Just so you know what the problem is that should be

```auto
function alertFunc() {
node.send( msg );
}

```
