# Invoke every user defined time

**URL:** <https://discourse.nodered.org/t/invoke-every-user-defined-time/896>\
**Category:** Dashboard\
**Created:** [18 June 2018 19:16 UTC](https://discourse.nodered.org/t/invoke-every-user-defined-time/896 "2018-06-18T19:16:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![natanel](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@natanel](https://discourse.nodered.org/u/natanel)\
**Post date:** [18 June 2018 19:16 UTC](https://discourse.nodered.org/t/invoke-every-user-defined-time/896/1 "2018-06-18T19:16:53Z")

</div>

Hi

I need to invoke a function every x second, which is provided from string node

the invoke node is only run every pre-defined time (not user control)

so I

1. reading for dashboard user time on/off and seconds and saving it in  
global.set(“userTimeSec”,msg.payload)  
global.set(“userTimeOnOff”,msg.payload)
2. invoke every 1 sec
3. invoke connected it switch and using it with expression condition
4. some thing like that  
$globalContext(‘userTimeOnOff’) & (( $milis()/1000 % $globalContext(‘userTimeSec’) ) = 0)

this is not working

1. what is the correct way to write the expression condition
2. is there better way?

BR

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [18 June 2018 19:26 UTC](https://discourse.nodered.org/t/invoke-every-user-defined-time/896/2 "2018-06-18T19:26:23Z")

</div>

white your function regarding global.set on a function node and just inject every second whatever as input that will run your function every second.

However I don’t see the point why u wanted to write a global variable every second instead of just do it whenever the value has changed, don’t see the sense on doing it.

Regrads

---

<div class="post-metadata">

**Author:** ![natanel](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@natanel](https://discourse.nodered.org/u/natanel)\
**Post date:** [18 June 2018 19:42 UTC](https://discourse.nodered.org/t/invoke-every-user-defined-time/896/3 "2018-06-18T19:42:03Z")

</div>

this is not what I ment

- the user insert time secX in second for running some functionX node
- from this point the functionX shold run every secX
- untill user chabge secX

this is done

set time

- user write time in dashboard text-input
- this time is saved in global

use time

- another flow use invoke every second
- which connected to switch
- which should use expression condition to check if to call next functioX node
- the expression condition is not working for me, probably because I do not know the syntex  
$globalContext(‘userTimeOnOff’) & (( $milis()/1000 % $globalContext(‘userTimeSec’) ) = 0)

any suggestion ?  
any better way?

thanks

---

<div class="post-metadata">

**Author:** ![davidcgu](https://avatars.discourse-cdn.com/v4/letter/d/e8c25b/32.png) [@davidcgu](https://discourse.nodered.org/u/davidcgu)\
**Post date:** [18 June 2018 19:55 UTC](https://discourse.nodered.org/t/invoke-every-user-defined-time/896/4 "2018-06-18T19:55:16Z")

</div>

Take a look on the forum, I guess there is valuable information for what u wanted to achieve on this thread.

> [@The DELAY node. Not doing what I want](https://discourse.nodered.org/t/the-delay-node-not-doing-what-i-want/854/21):
>
> Yes, and again: Thanks for the help. Ok, to re-cap: You did what I did in another case where you sent the value in the payload - given - but tagged the message with the topic. I was mistaken for a long time that it was actually msg.delay which is what the node wants. But you “cheated” and until the last second it was the topic and you moved the msg.topic -\> msg.delay() Got it. I am really wanting to get out of using the old context format, but so many of my nodes have that in them and so …

Regards

---

<div class="post-metadata">

**Author:** ![natanel](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@natanel](https://discourse.nodered.org/u/natanel)\
**Post date:** [18 June 2018 20:22 UTC](https://discourse.nodered.org/t/invoke-every-user-defined-time/896/5 "2018-06-18T20:22:43Z")

</div>

thanks  
tired to read, still missing some basic to understand

any way tell me if I am wrong  
the delay only delay the message for x sec not invoking a message every x sec

so if I want to do it with delay I will need  
invoke every 1sec -\> delay message userSeconds = 5 -\> stack over flow?, message are thrown away?

Regards

---

<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:** [19 June 2018 09:51 UTC](https://discourse.nodered.org/t/invoke-every-user-defined-time/896/6 "2018-06-19T09:51:29Z")

</div>

yes you will most likely get a stack overflow.

Maybe if you explain what you are doing this for people might be able to give you some other ideas.

---

<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:** [19 June 2018 10:54 UTC](https://discourse.nodered.org/t/invoke-every-user-defined-time/896/7 "2018-06-19T10:54:34Z")

</div>

If I understand correctly you want to repeat a message at a variable interval. If so then this flow demonstrates one way of doing that.

```auto
[{"id":"79629368.aee43c","type":"function","z":"6dc690a3.1abc88","name":"Repeater","func":"// Repeatedly sends the last message passed in\n// pass message with topic \"interval\" to specify repeate time in ms \n\nif (msg.topic === \"interval\") {\n context.set(\"interval\", msg.payload);\n} else {\n // and save it in the node context\n context.set(\"msg\", msg);\n}\n// have we got a message to repeat?\nmsg = context.get(\"msg\");\nif (msg) {\n // make a local clone of the message to pass on\n var msgClone = RED.util.cloneMessage(msg);\n var repeatInterval = context.get(\"interval\");\n if (repeatInterval === undefined) repeatInterval = 5000; // default interval ms\n // pass the message on\n node.send(msgClone);\n // loop at given interval re-sending it\n var tick = context.get('tick') || 0;\n // clear the repeater and restart\n if (tick) {\n \tclearInterval(tick);\n }\n tick = setInterval(function() {\n \tnode.send(msgClone);\n }, repeatInterval); // re-send at appropriate rate\n context.set('tick', tick);\n}\nreturn null;","outputs":1,"noerr":0,"x":363,"y":57,"wires":[["255b6bab.07560c"]]},{"id":"2cb59265.11dcee","type":"inject","z":"6dc690a3.1abc88","name":"","topic":"interval","payload":"2000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":129,"y":35,"wires":[["79629368.aee43c"]]},{"id":"4ca75529.d5e9dc","type":"inject","z":"6dc690a3.1abc88","name":"","topic":"","payload":"Message to be repeated","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":152,"y":145,"wires":[["79629368.aee43c"]]},{"id":"d5b5c9dd.d867c","type":"inject","z":"6dc690a3.1abc88","name":"","topic":"interval","payload":"3000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":73,"wires":[["79629368.aee43c"]]},{"id":"255b6bab.07560c","type":"debug","z":"6dc690a3.1abc88","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":554,"y":59,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![natanel](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@natanel](https://discourse.nodered.org/u/natanel)\
**Post date:** [19 June 2018 16:00 UTC](https://discourse.nodered.org/t/invoke-every-user-defined-time/896/8 "2018-06-19T16:00:26Z")

</div>

I need to send every user define seconds discovery message to iqrf mesh

The send discovery work with out problem  
( node red to mqtt to iqrf demon to transmitor to network)

The issue is how to invoke it every user define seconds

Thanks

---

<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:** [19 June 2018 16:29 UTC](https://discourse.nodered.org/t/invoke-every-user-defined-time/896/9 "2018-06-19T16:29:53Z")

</div>

Is that not what my flow does?

---

<div class="post-metadata">

**Author:** ![natanel](https://avatars.discourse-cdn.com/v4/letter/n/e99b99/32.png) [@natanel](https://discourse.nodered.org/u/natanel)\
**Post date:** [19 June 2018 17:38 UTC](https://discourse.nodered.org/t/invoke-every-user-defined-time/896/10 "2018-06-19T17:38:35Z")

</div>

Probably yes  
I will check as soon as possible  
Thank you
