# Receiving input to function node while the function is running

**URL:** https://discourse.nodered.org/t/receiving-input-to-function-node-while-the-function-is-running/24153
**Category:** General
**Created:** [3 April 2020 12:08 UTC](https://discourse.nodered.org/t/receiving-input-to-function-node-while-the-function-is-running/24153 "2020-04-03T12:08:21Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![mansahlgren](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mansahlgren](https://discourse.nodered.org/u/mansahlgren)
#### Post date: [3 April 2020 12:08 UTC](https://discourse.nodered.org/t/receiving-input-to-function-node-while-the-function-is-running/24153/1 "2020-04-03T12:08:21Z")

</div>

I want to receive input to a function that is already running without restarting the function, and I want this to be able to happen an arbitrary amount of times during the functions execution.

I tried using the flow context to set a variable in a function f1, and then reading it asynchronously in my function f2. It didnt work as I wanted; when I try to read the variable it will read it right away and not wait for it to be initialized or created, despite the fact that I delete the variable with a change node every time I retry.

Any ideas?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [3 April 2020 12:13 UTC](https://discourse.nodered.org/t/receiving-input-to-function-node-while-the-function-is-running/24153/2 "2020-04-03T12:13:01Z")

</div>

Hi, by the sounds of it, you are looping or running a constant process in a function node? this is not the ideal (events based) methodology that node-red is most commonly used for.

Perhaps if you share a commented / sanitised snipped of your flow we can help more.

NOTES:  
to export flow, select the NODEs to export, press CTRL+E, copy to clipboard then paste into your reply between three back ticks like this...

`````  
`code paste here`  
`````

---

<div class="post-metadata">

### Author: ![mansahlgren](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mansahlgren](https://discourse.nodered.org/u/mansahlgren)
#### Post date: [3 April 2020 12:21 UTC](https://discourse.nodered.org/t/receiving-input-to-function-node-while-the-function-is-running/24153/3 "2020-04-03T12:21:34Z")

</div>

Thanks for the fast answere. I am using one function node that uses several JS-timers and timeouts (setTimeout() and setInterval()) to create a "long lived" simulation.

This is a snippet:

```auto
    generateForkliftSpeedId = setInterval(generateForkliftSpeed, telemetryIntervalMoving);
    setTimeout(function() {
        clearInterval(generateForkliftSpeedId); //Stop speed generator.
        generateForksRisingTelemetryId = setInterval(generateForksRisingTelemetry, 
        telemetryIntervalMovingForks);
    }, timeToMove/2); //Forklift stopping to load.

```

This is the whole function code:

```auto
[{"id":"425c9e02.96f3","type":"function","z":"3a02ebf.434c414","name":"GenerateTelemetryLaglyftare0","func":"var timeToComplete = msg.payload.timeToComplete * 1000; //ms\nvar timeToMove = timeToComplete - 1000; //ms\nvar telemetryIntervalMoving = 1000;\nvar telemetryIntervalMovingForks=1000;\nvar palletSrcHeight = msg.payload.palletSrcHeight;\nvar palletWeight = msg.payload.palletWeight;\n\nvar forkliftSpeed, forkSpeed = 1, forksIncline=0, forkLoadWeight=0, forkHeightPos = 0, forkliftTippingPoint=12.5;\nvar forkliftSpeedUpperBound = 5; // m/s\nvar forkliftSpeedLowerBound = 1;\nvar tippingRate=4;//4 degrees per unit time.\n\nvar weightCapacity = 600;\nvar heightCapacity=5;\nvar forkliftHeightCapacityBreakingPoint=(weightCapacity/palletWeight)*heightCapacity;//Point where the forklifts start to lean.\nnode.send({payload:forkliftHeightCapacityBreakingPoint});\n\nvar generateForkliftSpeedId, generateForksRisingTelemetryId, generateForkliftLoweringTelemetryId;\n\nfunction generateForkliftSpeed(){\n forkliftSpeed = Math.floor(Math.random() * (forkliftSpeedUpperBound - forkliftSpeedLowerBound + 1)) + forkliftSpeedLowerBound;\n var testMsg = {};\n testMsg.payload = \"Speed: \" + forkliftSpeed;\n node.send(testMsg);\n}\n\nfunction loadPalletAndStartLoweringForks(){\n forkHeightPos=palletSrcHeight;\n clearInterval(generateForksRisingTelemetryId); //Stop lifting forks.\n forkLoadWeight=palletWeight;\n generateForkliftLoweringTelemetryId = setInterval(function(){generateForkliftLoweringTelemetry(true)}, telemetryIntervalMovingForks);\n}\n\nfunction generateForksRisingTelemetry(){\n forkHeightPos += forkSpeed*(telemetryIntervalMovingForks/1000);\n \n //Conditional stop.\n if (forkHeightPos>=heightCapacity && palletSrcHeight>heightCapacity){\n //Forks above capacity, capacity can NOT handle the height.\n forkHeightPos=heightCapacity;\n clearInterval(generateForksRisingTelemetryId); //Stop lifting forks.\n generateForkliftLoweringTelemetryId = setInterval(function(){generateForkliftLoweringTelemetry(false)}, telemetryIntervalMovingForks);\n }else if (forkHeightPos>=palletSrcHeight && palletSrcHeight<heightCapacity){\n //Forks above height, capacity can handle the height.\n loadPalletAndStartLoweringForks();\n }else if (forkHeightPos>=heightCapacity && heightCapacity==palletSrcHeight){\n //Forks above capacity and height, which are the same. Capacity can handle the height.\n loadPalletAndStartLoweringForks();\n }\n node.send({payload:{forkHeight: forkHeightPos, forkCap: heightCapacity, itemHeight: palletSrcHeight}});\n}\n\n/*\n* If the truck doesn't have a pallet, it lowers its forks to 0.\n* If the truck has a pallet that weighs to much for the truck\n* it will lower the pallet to the appropriate height if it gets\n* a command from the GGC. If the pallet weights to much the forklift\n* will start to lean. However, if the truck\n* can handle the weight at the palletSrcHeight, it will keep\n* it there and the function does nothing.\n*/\nfunction generateForkliftLoweringTelemetry(hasPallet){\n \n if (!hasPallet){\n forkHeightPos -= forkSpeed*(telemetryIntervalMovingForks/1000); //Convert to seconds\n //Forklift is free, report back so that the forklift can take a new job.\n if (forkHeightPos<=0){//Adjust the forks.\n forkHeightPos=0;//Lift forks a little.\n clearInterval(generateForkliftLoweringTelemetryId); //Stop lowering forks.\n node.send({payload:\"No pallet\"});\n }\n }else{\n //Forklift starts to lean.\n if (forkHeightPos>=forkliftHeightCapacityBreakingPoint){\n forksIncline+=tippingRate;\n node.send({payload:\"Forklift leaning\"});\n //Forklift tipped over.\n if (forksIncline>=forkliftTippingPoint){\n clearInterval(generateForkliftLoweringTelemetryId); //Stop generate lowering telemetry.\n node.send({payload:\"Forklift tipped over!\"});\n }\n \n }else{\n //Forklift keep moving to destination.\n node.send({payload:\"Forklift moving to dest\"});\n clearInterval(generateForkliftLoweringTelemetryId); //Stop lowering forks.\n generateForkliftSpeedId = setInterval(generateForkliftSpeed, telemetryIntervalMoving);\n setTimeout(function() { //Anonymous function.\n clearInterval(generateForkliftSpeedId); //Stop speed generator.Forklift job done, report back.\n }, timeToMove/2); //Forklift stopping to load.\n }\n }\n \n node.send({payload:forkHeightPos});\n}\n\nfunction main(){\n flow.get('instr0', function(err, instr0){\n if (err){\n node.error(err, msg);\n node.send({payload:\"ERROR\"});\n }else{\n switch (instr0){\n case 0:\n node.send({payload:\"Instruction was 0!!!!\"});\n break;\n case 1:\n node.send({payload:\"Lowering forks!!!!!!!!!!!!!\"});\n break;\n case 2:\n break;\n case 3:\n break;\n default:\n node.send({payload:instr0});\n break;\n }\n flow.set('instr0', 0);\n }\n });\n generateForkliftSpeedId = setInterval(generateForkliftSpeed, telemetryIntervalMoving);\n setTimeout(function() { //Anonymous function.\n clearInterval(generateForkliftSpeedId); //Stop speed generator.\n generateForksRisingTelemetryId = setInterval(generateForksRisingTelemetry, telemetryIntervalMovingForks);\n }, timeToMove/2); //Forklift stopping to load.\n}\n\nmain();\n\n\nreturn msg;","outputs":1,"noerr":0,"x":1610,"y":160,"wires":[["89c570e0.ff0ec"]]}]

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [3 April 2020 12:29 UTC](https://discourse.nodered.org/t/receiving-input-to-function-node-while-the-function-is-running/24153/4 "2020-04-03T12:29:27Z")

</div>

Yeah, as i suspected, you've written this like a procedural program - IMO, that's your issue here.

I would recommend avoid using setTimeouts/setIntervals in functions and use inject nodes etc for your timings. Pass the events with a topic name to identify the timed events into your function, picking up any changes you need beforehand to "mix in" required data into the `msg` object.

in fact, it looks more like a state machine I would also look at [these nodes](https://flows.nodered.org/search?term=state+machin&type=node&type=flow&type=collection)

Hope that helps.

---

<div class="post-metadata">

### Author: ![mansahlgren](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mansahlgren](https://discourse.nodered.org/u/mansahlgren)
#### Post date: [3 April 2020 12:36 UTC](https://discourse.nodered.org/t/receiving-input-to-function-node-while-the-function-is-running/24153/5 "2020-04-03T12:36:12Z")

</div>

Ah, okay, I think I understand. So for example, I trigger interval based injections when something particular happens in the flow. When you say "events with a topic name", do you really mean "messages with a topic name"?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [3 April 2020 12:37 UTC](https://discourse.nodered.org/t/receiving-input-to-function-node-while-the-function-is-running/24153/6 "2020-04-03T12:37:49Z")

</div>

> [@mansahlgren](#):
>
> When you say "events with a topic name", do you really mean "messages with a topic name"?

Sure, same thing (an event is something that happens, the `msg` is what is sent)

> [@mansahlgren](#):
>
> I trigger interval based injections when something particular happens in the flow

Yes, that is possible, you can also use an `inject` node to generate periodic / timed events

the basic premise is that each node does a little bit work & you lay it all out graphically (instead of one big program in a function node. TBH, if you write a program like that, you might as well have written a straight node.js program as you wont benefit from the huge benefits of the "flow programming paradigm")

---

<div class="post-metadata">

### Author: ![mansahlgren](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mansahlgren](https://discourse.nodered.org/u/mansahlgren)
#### Post date: [3 April 2020 12:39 UTC](https://discourse.nodered.org/t/receiving-input-to-function-node-while-the-function-is-running/24153/7 "2020-04-03T12:39:47Z")

</div>

I see, thank you!

---

<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: [2 June 2020 12:39 UTC](https://discourse.nodered.org/t/receiving-input-to-function-node-while-the-function-is-running/24153/8 "2020-06-02T12:39:53Z")

</div>

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