# Delay in Function

**URL:** <https://discourse.nodered.org/t/delay-in-function/8950>\
**Category:** General\
**Created:** [14 March 2019 00:32 UTC](https://discourse.nodered.org/t/delay-in-function/8950 "2019-03-14T00:32:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derluchs](https://avatars.discourse-cdn.com/v4/letter/d/85e7bf/32.png) [@Derluchs](https://discourse.nodered.org/u/Derluchs)\
**Post date:** [14 March 2019 00:32 UTC](https://discourse.nodered.org/t/delay-in-function/8950/1 "2019-03-14T00:32:21Z")

</div>

Hello everybody i know this Theme About Adding a delay in a function but im new and i Need some help.

In my Function "Katze kommt/geht " i Need a delay of 5Sec after the return of the msg1 and msg2. So only the first Sensor who was activatet will return the msg and after the 5Seconds ist possible that the second Sensor activates = return msg2 = 5sec delay.

i hope someone can help me thank you and sry for the bad english 😕

[{"id":"dca71496.fc34d8","type":"tab","label":"Corbaz","disabled":false,"info":""},{"id":"9868ba65.a0e578","type":"trigger","z":"dca71496.fc34d8","op1":"","op2":"","op1type":"pay","op2type":"nul","duration":"5","extend":false,"units":"s","reset":"","bytopic":"all","name":"Set Trigger","x":450.14849853515625,"y":125.64457511901855,"wires":[["327a180e.7f0d08"]]},{"id":"3ff08023.f2ead","type":"trigger","z":"dca71496.fc34d8","op1":"","op2":"","op1type":"pay","op2type":"nul","duration":"5","extend":false,"units":"s","reset":"","bytopic":"all","name":"Set Trigger","x":337.01953887939453,"y":331.0039482116699,"wires":[["327a180e.7f0d08"]]},{"id":"f55e4cf4.d396c","type":"ui\_text","z":"dca71496.fc34d8","group":"b547b27f.d5b82","order":0,"width":0,"height":0,"name":"Sensor auslesen","label":"Katzen im Haus","format":"{{msg.payload}}","layout":"row-spread","x":764.1487083435059,"y":238.32819366455078,"wires":},{"id":"9cefb4d8.1a3638","type":"rpi-srf","z":"dca71496.fc34d8","name":"Sensor Aussen","topic":"Aussen","pulse":"1","pins":"11,12","x":107.01953125,"y":34.003926277160645,"wires":[["ab8d24f2.2d07d8"]]},{"id":"1b27ac8c.f67de3","type":"rpi-srf","z":"dca71496.fc34d8","name":"Sensor Innen","topic":"Aussen","pulse":"1","pins":"3,5","x":79.01953125,"y":450.0039176940918,"wires":[["46755109.e33ea"]]},{"id":"328128e0.236cc8","type":"function","z":"dca71496.fc34d8","name":"set Globale variable Sensoraussen","func":"global.set("Sensoraussen",msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":405.01953125,"y":44.003915786743164,"wires":[["9868ba65.a0e578"]]},{"id":"f72d2d12.c3529","type":"function","z":"dca71496.fc34d8","name":"set Globale variable Sensoraussen","func":"global.set("Sensorinnen",msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":271.01953125,"y":388.00392150878906,"wires":[["3ff08023.f2ead"]]},{"id":"ab8d24f2.2d07d8","type":"switch","z":"dca71496.fc34d8","name":"\<=15","property":"payload","propertyType":"msg","rules":[{"t":"btwn","v":"0","vt":"num","v2":"10","v2t":"num"}],"checkall":"true","repair":false,"outputs":1,"x":99.14063262939453,"y":102.0703182220459,"wires":[["328128e0.236cc8"]]},{"id":"46755109.e33ea","type":"switch","z":"dca71496.fc34d8","name":"\<=15","property":"payload","propertyType":"msg","rules":[{"t":"btwn","v":"0","vt":"num","v2":"10","v2t":"num"}],"checkall":"true","repair":false,"outputs":1,"x":127.01953125,"y":340.00390625,"wires":[["f72d2d12.c3529"]]},{"id":"bf644e00.1cfcb","type":"debug","z":"dca71496.fc34d8","name":"katze kommt","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":679.14453125,"y":135.453125,"wires":},{"id":"327a180e.7f0d08","type":"function","z":"dca71496.fc34d8","name":"Katze kommt/geht ","func":"var Sensoraussen1 = global.get("Sensoraussen");\nmsg.payload = Sensoraussen1;\nvar Sensorinnen1 = global.get("Sensorinnen");\nmsg.payload = Sensorinnen1;\nvar msg1 = {payload: "Katze kommt"};\nvar msg2 = {payload: "Katze geht"};\nvar myVar;\n\nif (Sensoraussen1 \> Sensorinnen1){\n \n return (msg1);\n}\nelse if (Sensorinnen1 \> Sensoraussen1){\n \n return (msg2);\n }","outputs":2,"noerr":0,"x":521.0196228027344,"y":230.00392627716064,"wires":[["f55e4cf4.d396c","bf644e00.1cfcb"],["a5621543.4eecd8"]]},{"id":"a5621543.4eecd8","type":"debug","z":"dca71496.fc34d8","name":"katze geht","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":722.01953125,"y":299.00390625,"wires":},{"id":"b625bc1c.7fed7","type":"function","z":"dca71496.fc34d8","name":"Katze kommt/geht ","func":"var Sensoraussen1 = global.get("Sensoraussen");\nmsg.payload = Sensoraussen1;\nvar Sensorinnen1 = global.get("Sensorinnen");\nmsg.payload = Sensorinnen1;\nvar msg1 = {payload: "Katze kommt"};\nvar msg2 = {payload: "Katze geht"};\n\nif (Sensoraussen1 \> Sensorinnen1) {\n while (Sensoraussen1 \> Sensorinnen1)\n return [msg1,null];\n}\nelse if (Sensorinnen1 \> Sensoraussen1){\n while (Sensorinnen1 \> Sensoraussen1)\n return [null,msg2];\n}","outputs":2,"noerr":0,"x":806.01953125,"y":536.00390625,"wires":[,]},{"id":"b547b27f.d5b82","type":"ui\_group","z":"","name":"Sensor auslesen","tab":"e00e648.58db998","disp":true,"width":"6","collapse":false},{"id":"e00e648.58db998","type":"ui\_tab","z":"","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

---

<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:** [14 March 2019 00:56 UTC](https://discourse.nodered.org/t/delay-in-function/8950/2 "2019-03-14T00:56:59Z")

</div>

Hello @Derluchs, welcome to the Node-RED forum

Please have a look in this topic on how to share flows in the forum. We need that you reformat yor post so we can import and test your flow to provide the best advice.

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

---

<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:** [14 March 2019 10:39 UTC](https://discourse.nodered.org/t/delay-in-function/8950/3 "2019-03-14T10:39:41Z")

</div>

> [@Derluchs](#):
>
> In my Function "Katze kommt/geht " i Need a delay of 5Sec after the return of the msg1 and msg2. So only the first Sensor who was activatet will return the msg and after the 5Seconds ist possible that the second Sensor activates = return msg2 = 5sec delay.

In general, you need to use the async setTimeout function to do this. It is standard JavaScript. You can use node.send inside the function so that it is sent at the right point.

> **[JavaScript Timing Events](https://www.w3schools.com/js/js_timing.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

---

<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:** [14 March 2019 14:48 UTC](https://discourse.nodered.org/t/delay-in-function/8950/4 "2019-03-14T14:48:53Z")

</div>

If you send them on different outputs from the function node then you can use a Delay node to delay one of them.

---

<div class="post-metadata">

**Author:** ![Derluchs](https://avatars.discourse-cdn.com/v4/letter/d/85e7bf/32.png) [@Derluchs](https://discourse.nodered.org/u/Derluchs)\
**Post date:** [14 March 2019 17:23 UTC](https://discourse.nodered.org/t/delay-in-function/8950/5 "2019-03-14T17:23:49Z")

</div>

this isnt working because one sensor is on the front of a door and the other one on the back and i want to Count with a Ultrasonic sensor how many people are comming in or going out.  
When Sensor1 \> Sensor 2 return +1  
when Sensor2 \> Sensor 1 return -1

---

<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:** [14 March 2019 17:37 UTC](https://discourse.nodered.org/t/delay-in-function/8950/6 "2019-03-14T17:37:17Z")

</div>

I think you need to explain the sequence of events and what the data is in order that we can understand what you are asking.
