# Functionnode: If loop with time

**URL:** <https://discourse.nodered.org/t/functionnode-if-loop-with-time/13546>\
**Category:** General\
**Created:** [22 July 2019 11:11 UTC](https://discourse.nodered.org/t/functionnode-if-loop-with-time/13546 "2019-07-22T11:11:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kiwi\_gamer01](https://avatars.discourse-cdn.com/v4/letter/k/a587f6/32.png) [@Kiwi\_gamer01](https://discourse.nodered.org/u/Kiwi_gamer01)\
**Post date:** [22 July 2019 11:11 UTC](https://discourse.nodered.org/t/functionnode-if-loop-with-time/13546/1 "2019-07-22T11:11:12Z")

</div>

I have written this function that if the value x for a certain time  
(Time1) is higher than the value y. All values ​​are numbers. After I'm not familiar with javascript I do not know why the if loop with the counter does not work

```auto
x = msg.payload.down
y = msg.topic.topic1
time = msg.payload.Time1
z = 0

while (x >= y) {
    if (z >= time){
        msg.payload.Was = "Jetzt"
    } 
    else {
        z += 1
    }
}
while (x < y) {
    z = 0
{
return msg;

```

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [22 July 2019 11:31 UTC](https://discourse.nodered.org/t/functionnode-if-loop-with-time/13546/2 "2019-07-22T11:31:12Z")

</div>

> [@Kiwi\_gamer01](#):
>
> while (x \>= y) { if (z \>= time){ msg.payload.Was = "Jetzt" } else { z += 1 }

So you are comparing x against y but don't change either x or y so the condition you are testing will never change.

> while (x \< y) {  
> z = 0  
> {  
> same thing and you need to close your while statement. (the function node should have flagged this with a red cross indicating a problem...)

See [JavaScript while Loop](https://www.w3schools.com/js/js_loop_while.asp)

---

<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:** [22 July 2019 11:45 UTC](https://discourse.nodered.org/t/functionnode-if-loop-with-time/13546/3 "2019-07-22T11:45:38Z")

</div>

I find if you talk yourself through your code you will spot problems...

```auto
x = msg.payload.down //didnt declare x. Should be var x or let x
y = msg.topic.topic1 //didnt declare y. Should be var y or let y
time = msg.payload.Time1 //again no declare
z = 0 //you get the idea

while (x >= y) { //so loop round and round and round until x is > or = to y
    if (z >= time){
        msg.payload.Was = "Jetzt"
    } 
    else {
        z += 1
    }
} //end of loop - but hang on, x never changed - this will loop for ever!

while (x < y) {//again, loop round until x < y. where do you change x ?
    z = 0
{
return msg; 
  // <<-- missing a close brace here!

```

I suspect you think a new payload will update x and y.

You are not thinking in terms of node-red flow programming me thinks.

Perhaps you should describe what you are trying to achieve?  
Perhaps we can guide you?

---

<div class="post-metadata">

**Author:** ![Kiwi\_gamer01](https://avatars.discourse-cdn.com/v4/letter/k/a587f6/32.png) [@Kiwi\_gamer01](https://discourse.nodered.org/u/Kiwi_gamer01)\
**Post date:** [22 July 2019 12:56 UTC](https://discourse.nodered.org/t/functionnode-if-loop-with-time/13546/4 "2019-07-22T12:56:46Z")

</div>

Thanks, yes I could use help and I hope it is understandable what I explain below.

I have a photodiode which gives me a number value of the voltage of the diode. And also I have a dashboard on which one set the desired voltage at which commanded (shutters down) to be executed. But the command he should be executed when the value of the photodiode for a certain time (Time1) falls below the value of a Dashboardslider

I will send you the exportet file later, if you need it.

---

<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:** [22 July 2019 13:02 UTC](https://discourse.nodered.org/t/functionnode-if-loop-with-time/13546/5 "2019-07-22T13:02:02Z")

</div>

it will be helpful to post screen shots of what you have done so far & the flow.

_Remember to paste your exported flow between three backticks e.g._

`````  
`your flow here`  
`````

However, some pointers first...

- Checkout the trigger & delay node (read the info on the side-bar for clues)
- Get rid of the loop in the function
  - Not least because you will cause an infinite loop and cause other issues

---

<div class="post-metadata">

**Author:** ![Kiwi\_gamer01](https://avatars.discourse-cdn.com/v4/letter/k/a587f6/32.png) [@Kiwi\_gamer01](https://discourse.nodered.org/u/Kiwi_gamer01)\
**Post date:** [22 July 2019 13:06 UTC](https://discourse.nodered.org/t/functionnode-if-loop-with-time/13546/6 "2019-07-22T13:06:45Z")

</div>

The problem is just that I can not connect to the Node red editor, which means I can not make screenshots or similar.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [22 July 2019 14:36 UTC](https://discourse.nodered.org/t/functionnode-if-loop-with-time/13546/7 "2019-07-22T14:36:05Z")

</div>

Probably because you flow/function is in an infinite loop!  
So stop node-red and restart Node-RED with the --safe setting which starts node-red, loads your flow but doesn't deploy.
