# Creating threads in mp-function

**URL:** <https://discourse.nodered.org/t/creating-threads-in-mp-function/63088>\
**Category:** General\
**Tags:** function-node\
**Created:** [26 May 2022 05:36 UTC](https://discourse.nodered.org/t/creating-threads-in-mp-function/63088 "2022-05-26T05:36:47Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![noob69](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@noob69](https://discourse.nodered.org/u/noob69)\
**Post date:** [26 May 2022 05:36 UTC](https://discourse.nodered.org/t/creating-threads-in-mp-function/63088/1 "2022-05-26T05:36:47Z")

</div>

Hi,

I required to create threads using mp-function node.I can't find any documentation about that. What I need is create multiple dealys according to values in a array.Is there any other methods to achieve that.

Thanks.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 May 2022 08:04 UTC](https://discourse.nodered.org/t/creating-threads-in-mp-function/63088/2 "2022-05-26T08:04:53Z")

</div>

Not used the mp-function node, I presume it's uses Javascript like the function node. If so you Can use [setTimeout method](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) to create delays in the JS and use node.send to output the messages. If you need more input please explain what you are trying to achieve, with examples of input and out you require.

---

<div class="post-metadata">

**Author:** ![noob69](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@noob69](https://discourse.nodered.org/u/noob69)\
**Post date:** [26 May 2022 08:08 UTC](https://discourse.nodered.org/t/creating-threads-in-mp-function/63088/3 "2022-05-26T08:08:50Z")

</div>

Hi, I actually want create custom delays according to values in a array. Array contains multiple messages with multiple delays. So I guess this task required threading.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [26 May 2022 08:35 UTC](https://discourse.nodered.org/t/creating-threads-in-mp-function/63088/4 "2022-05-26T08:35:57Z")

</div>

Note, `node-red-contrib-mp-function` was last published 4 years 10 months ago (as of the date of this post) and as you have discovered, there is not much documentation.

Not saying it won't work, but if anything goes wrong, it looks like you are on your own

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 May 2022 09:48 UTC](https://discourse.nodered.org/t/creating-threads-in-mp-function/63088/5 "2022-05-26T09:48:54Z")

</div>

Yes still possible in javascript with timeout() and node.send(), Or use split node and change node, you can probably do this without using a function node.

---

<div class="post-metadata">

**Author:** ![noob69](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@noob69](https://discourse.nodered.org/u/noob69)\
**Post date:** [26 May 2022 10:44 UTC](https://discourse.nodered.org/t/creating-threads-in-mp-function/63088/6 "2022-05-26T10:44:34Z")

</div>

@E1cid Is it possible for multiple and various intervals that stored in a array.

---

<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:** [26 May 2022 11:13 UTC](https://discourse.nodered.org/t/creating-threads-in-mp-function/63088/7 "2022-05-26T11:13:16Z")

</div>

> [@noob69](#):
>
> Is it possible for multiple and various intervals that stored in a array

Anything is possible.

> [@noob69](#):
>
> various intervals

> [@noob69](#):
>
> create multiple dealys

This is all a bit vague.

Maybe if you post a sample array and explain what you wish to happen we could offer you suitable advice.

---

<div class="post-metadata">

**Author:** ![noob69](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@noob69](https://discourse.nodered.org/u/noob69)\
**Post date:** [26 May 2022 14:40 UTC](https://discourse.nodered.org/t/creating-threads-in-mp-function/63088/8 "2022-05-26T14:40:39Z")

</div>

Hi, @Steve-Mcl  
Imagine there are values like following example stored in a database.

```auto
{id:1,alert:"humidity",interval:"10"}
{Id:2, alert:"level",interval:"10"}
{id:3, alert:"CO2",interval:"20"}

```

Inject node will request once a minute from database. Above values will retrieved from the database. When the values received to the node-server,I need to set intervals according to those values. And send messeges according to that interval.Also Inject node will keep requesting the values.

---

<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:** [26 May 2022 14:45 UTC](https://discourse.nodered.org/t/creating-threads-in-mp-function/63088/9 "2022-05-26T14:45:09Z")

</div>

What would the payload look like when these "intervals" occur?

Also, when you say interval - do you mean "every 10 seconds" or "once in 10 sec time"?

---

<div class="post-metadata">

**Author:** ![noob69](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@noob69](https://discourse.nodered.org/u/noob69)\
**Post date:** [26 May 2022 15:06 UTC](https://discourse.nodered.org/t/creating-threads-in-mp-function/63088/10 "2022-05-26T15:06:33Z")

</div>

payload will be a numeric value from another node. I just want to trigger it according to the interval. I need trigger it according once in a whatever value. There will be more than 3 datasets.If it is only for few values I could be able to simply filter it out from the id and pass it in to settimeout function.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 May 2022 15:30 UTC](https://discourse.nodered.org/t/creating-threads-in-mp-function/63088/11 "2022-05-26T15:30:20Z")

</div>

With a simple split and delay.

```auto
[{"id":"7af3efd.ed4cc1","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"id\":1,\"alert\":\"humidity\",\"interval\":\"10\"},{\"Id\":2,\"alert\":\"level\",\"interval\":\"10\"},{\"id\":3,\"alert\":\"CO2\",\"interval\":\"20\"}]","payloadType":"json","x":160,"y":1900,"wires":[["365e2541.1a7b6a"]]},{"id":"365e2541.1a7b6a","type":"split","z":"bf9e1e33.030598","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":290,"y":1900,"wires":[["49672b41.3fa4e4"]]},{"id":"49672b41.3fa4e4","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"$number($$.payload.interval)*1000","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"payload.alert","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":1900,"wires":[["bf80a3d9.252c08"]]},{"id":"bf80a3d9.252c08","type":"delay","z":"bf9e1e33.030598","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":640,"y":1900,"wires":[["5f3d4c9f.1b6314"]]},{"id":"5f3d4c9f.1b6314","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":1980,"wires":[]}]

```

or function

```auto
[{"id":"7c9f48df.7dfa58","type":"function","z":"bf9e1e33.030598","name":"","func":"msg.payload.forEach(obj => {\n setTimeout(() => {\n node.send({payload:obj})\n}, Number(obj.interval)*1000)\n})\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":1900,"wires":[["5f3d4c9f.1b6314"]]},{"id":"7af3efd.ed4cc1","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"id\":1,\"alert\":\"humidity\",\"interval\":\"10\"},{\"Id\":2,\"alert\":\"level\",\"interval\":\"10\"},{\"id\":3,\"alert\":\"CO2\",\"interval\":\"20\"}]","payloadType":"json","x":160,"y":1900,"wires":[["7c9f48df.7dfa58"]]},{"id":"5f3d4c9f.1b6314","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":1980,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![noob69](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@noob69](https://discourse.nodered.org/u/noob69)\
**Post date:** [26 May 2022 16:51 UTC](https://discourse.nodered.org/t/creating-threads-in-mp-function/63088/12 "2022-05-26T16:51:55Z")

</div>

@E1cid That function is working nicely I think I have to create a connection with database as well otherwise it will ran 10 seconds agian without completing previous interval.
