sleep / wait function in javascript

3,497 views
Skip to first unread message

esc...@missionroom.nl

unread,
Feb 7, 2017, 2:41:03 PM2/7/17
to Node-RED
Trying to show a message for 2 seconds and then show other message.

Part of the script is this

        if ( code == 1233 ){
           msg.code = "BAM!";
       
           msg.code = "enter code";
           
       }



I tried :

function sleep (time) {
 return new Promise((resolve) => setTimeout(resolve, time));
}

------

        if ( code == 1233 ){
           msg.code = "BAM!";
           sleep(500).then(() => {
           msg.code = "plop!";
           });
       }


but that did nothing....
what am i doing wrong?

Dave C-J

unread,
Feb 7, 2017, 3:37:48 PM2/7/17
to node...@googlegroups.com
will the trigger node not do what you want ?

Nick O'Leary

unread,
Feb 7, 2017, 3:41:46 PM2/7/17
to Node-RED Mailing List
Is this code in a Function node or a custom node you are writing?

It's hard to help with so little information about where this code is being used.

Nick

On 7 February 2017 at 20:37, Dave C-J <dce...@gmail.com> wrote:
will the trigger node not do what you want ?

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/CACXWFwKHkSiqpscGTAa7TLNGD74zK%2BZHtaqBXJn01UHCAO31CQ%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

esc...@missionroom.nl

unread,
Feb 8, 2017, 3:13:03 AM2/8/17
to Node-RED
sorry i tried to much to be cloud and clear...

the setup is like this:
[{"id":"7b41cda8.4eb3a4","type":"function","z":"5cc18cb4.278284","name":"","func":"var code = msg.payload;\n\nif ( code == 1234 ){\n    msg.code = \"good!\";\n    node.log(\"good!\");\n\n} else {\n    msg.code = \"* wrong *\";\n    node.log(\"wrong\");\n    }\n    \nvar msg = {payload: code};\n\nreturn msg;","outputs":"1","noerr":0,"x":632.4285888671875,"y":270.7142791748047,"wires":[["39a61c8d.e4fe24","b3d06d15.d3fb68"]]},{"id":"39a61c8d.e4fe24","type":"debug","z":"5cc18cb4.278284","name":"","active":false,"console":"false","complete":"true","x":831.7142944335938,"y":244.85714721679688,"wires":[]},{"id":"b3d06d15.d3fb68","type":"ui_text","z":"5cc18cb4.278284","group":"a3c24ded.15fb9","order":1,"width":"3","height":"1","name":"","label":"","format":"{{msg.code}}","layout":"col-center","x":835.7142944335938,"y":289.8571472167969,"wires":[]},{"id":"42b9efec.bdabb","type":"inject","z":"5cc18cb4.278284","name":"","topic":"","payload":"1234","payloadType":"str","repeat":"","crontab":"","once":false,"x":445.71428571428567,"y":244.28571428571425,"wires":[["7b41cda8.4eb3a4"]]},{"id":"c0b276f.2f67708","type":"inject","z":"5cc18cb4.278284","name":"","topic":"","payload":"1111","payloadType":"str","repeat":"","crontab":"","once":false,"x":444.2857360839844,"y":297.1428527832031,"wires":[["7b41cda8.4eb3a4"]]},{"id":"a3c24ded.15fb9","type":"ui_group","z":"","name":"key","tab":"346cae52.82a11a","disp":false,"width":"3"},{"id":"346cae52.82a11a","type":"ui_tab","z":"","name":"1","icon":"dashboard","order":1}]

after entering the code it give a success or fail message
i want to show the result for only 2 seconds and then become empty again

Dave C-J

unread,
Feb 8, 2017, 3:48:18 AM2/8/17
to node...@googlegroups.com
again.. what is wrong with the trigger node ?

esc...@missionroom.nl

unread,
Feb 8, 2017, 4:06:05 AM2/8/17
to Node-RED
The code is doing the job it needs to do.
When entering a wrong code if gives false
But what i am trying to create is that the message false is only visible for 2 seconds.
I tried this with several delay and sleep functions but that didnt work out 

Op woensdag 8 februari 2017 09:48:18 UTC+1 schreef Dave C-J:

Dave C-J

unread,
Feb 8, 2017, 4:57:14 AM2/8/17
to node...@googlegroups.com
the code I sent shows the message to 2 seconds (good or error) then goes blank... is that not what is wanted ?

esc...@missionroom.nl

unread,
Feb 8, 2017, 5:16:35 AM2/8/17
to Node-RED
that is what i want, show the result for 2 seconds en then blank
But this does not work in the way i tried it.

Wich code did you send?

Op woensdag 8 februari 2017 10:57:14 UTC+1 schreef Dave C-J:

Mark Setrem

unread,
Feb 8, 2017, 6:08:11 AM2/8/17
to Node-RED
See the node-red flow at the bottom of Dave's Email that starts "again.. what is wrong with the trigger node ?"

You can import this into node-red by going Import > Clipboard

esc...@missionroom.nl

unread,
Feb 8, 2017, 6:27:41 AM2/8/17
to Node-RED
O it had some more mistakes, but now it is functioning with the code below.
Sorry...

But then my wish is that the result is displayed for 2 seconds and then blank


[{"id":"e3a3debd.8593d8","type":"function","z":"c33678df.aad368","name":"","func":"var code = msg.payload;\n\nif ( code == 1234 ){\n   var screen = \"good!\";\n    node.log(\"good!\");\n\n} else {\n   var screen = \"* wrong *\";\n    node.log(\"wrong\");\n    }\n    \nvar msg = {screen: screen};\n\nreturn msg;","outputs":"1","noerr":0,"x":588.5714111328125,"y":858.5714721679688,"wires":[["8f7672f9.b15088","c933afe7.baec58"]]},{"id":"8f7672f9.b15088","type":"debug","z":"c33678df.aad368","name":"","active":true,"console":"false","complete":"true","x":787.8571166992188,"y":832.7143402099609,"wires":[]},{"id":"c933afe7.baec58","type":"ui_text","z":"c33678df.aad368","group":"1cdb430d.21bd75","order":1,"width":"3","height":"1","name":"","label":"result","format":"{{msg.screen}}","layout":"row-spread","x":791.8571166992188,"y":877.7143402099609,"wires":[]},{"id":"bd391639.608fc","type":"inject","z":"c33678df.aad368","name":"","topic":"","payload":"1234","payloadType":"str","repeat":"","crontab":"","once":false,"x":401.8571079799108,"y":832.1429072788783,"wires":[["e3a3debd.8593d8"]]},{"id":"e918848d.8ecb58","type":"inject","z":"c33678df.aad368","name":"","topic":"","payload":"1111","payloadType":"str","repeat":"","crontab":"","once":false,"x":400.4285583496094,"y":885.0000457763672,"wires":[["e3a3debd.8593d8"]]},{"id":"51b91dde.112da4","type":"ui_button","z":"c33678df.aad368","name":"","group":"1cdb430d.21bd75","order":0,"width":0,"height":0,"label":"1234","color":"","bgcolor":"","icon":"","payload":"1234","payloadType":"str","topic":"","x":398.57141494750977,"y":785.7142724990845,"wires":[["e3a3debd.8593d8"]]},{"id":"c24dfe0a.603f78","type":"ui_button","z":"c33678df.aad368","name":"","group":"1cdb430d.21bd75","order":0,"width":0,"height":0,"label":"111","color":"","bgcolor":"","icon":"","payload":"1111","payloadType":"str","topic":"","x":401.42858123779297,"y":931.4286155700684,"wires":[["e3a3debd.8593d8"]]},{"id":"1cdb430d.21bd75","type":"ui_group","z":"","name":"Default","tab":"360388d6.389838","disp":true,"width":"6"},{"id":"360388d6.389838","type":"ui_tab","z":"","name":"testing","icon":"dashboard"}]



Op woensdag 8 februari 2017 12:08:11 UTC+1 schreef Mark Setrem:

Dave C-J

unread,
Feb 8, 2017, 6:34:08 AM2/8/17
to node...@googlegroups.com
I still don't see a trigger node in there... 

esc...@missionroom.nl

unread,
Feb 8, 2017, 6:35:59 AM2/8/17
to Node-RED
ok, so i need to do it with a trigger node?
I was trying to do it with a delay / or sleep function in javascript.

I get it, i will try to do it that way!

Op woensdag 8 februari 2017 12:34:08 UTC+1 schreef Dave C-J:

esc...@missionroom.nl

unread,
Feb 8, 2017, 6:48:10 AM2/8/17
to Node-RED
Got it running, thanks

[{"id":"e3a3debd.8593d8","type":"function","z":"c33678df.aad368","name":"","func":"var code = msg.payload;\n\nif ( code == 1234 ){\n   var screen = \"good!\";\n    node.log(\"good!\");\n\n} else {\n   var screen = \"* wrong *\";\n    node.log(\"wrong\");\n    }\n    \nvar msg = {screen: screen};\n\nreturn msg;","outputs":"1","noerr":0,"x":588.5714111328125,"y":858.5714721679688,"wires":[["8f7672f9.b15088","c933afe7.baec58","7f9fe1cc.af6c8"]]},{"id":"8f7672f9.b15088","type":"debug","z":"c33678df.aad368","name":"","active":true,"console":"false","complete":"true","x":787.8571166992188,"y":832.7143402099609,"wires":[]},{"id":"c933afe7.baec58","type":"ui_text","z":"c33678df.aad368","group":"1cdb430d.21bd75","order":1,"width":"3","height":"1","name":"","label":"result","format":"{{msg.screen}}","layout":"row-spread","x":791.8571166992188,"y":877.7143402099609,"wires":[]},{"id":"bd391639.608fc","type":"inject","z":"c33678df.aad368","name":"","topic":"","payload":"1234","payloadType":"str","repeat":"","crontab":"","once":false,"x":401.8571079799108,"y":832.1429072788783,"wires":[["e3a3debd.8593d8"]]},{"id":"e918848d.8ecb58","type":"inject","z":"c33678df.aad368","name":"","topic":"","payload":"1111","payloadType":"str","repeat":"","crontab":"","once":false,"x":400.4285583496094,"y":885.0000457763672,"wires":[["e3a3debd.8593d8"]]},{"id":"51b91dde.112da4","type":"ui_button","z":"c33678df.aad368","name":"","group":"1cdb430d.21bd75","order":0,"width":0,"height":0,"label":"1234","color":"","bgcolor":"","icon":"","payload":"1234","payloadType":"str","topic":"","x":398.57141494750977,"y":785.7142724990845,"wires":[["e3a3debd.8593d8"]]},{"id":"c24dfe0a.603f78","type":"ui_button","z":"c33678df.aad368","name":"","group":"1cdb430d.21bd75","order":0,"width":0,"height":0,"label":"111","color":"","bgcolor":"","icon":"","payload":"1111","payloadType":"str","topic":"","x":401.42858123779297,"y":931.4286155700684,"wires":[["e3a3debd.8593d8"]]},{"id":"7f9fe1cc.af6c8","type":"trigger","z":"c33678df.aad368","op1":"","op2":"1","op1type":"nul","op2type":"str","duration":"2","extend":false,"units":"s","reset":"","name":"","x":540,"y":999,"wires":[["bb454826.c1adf8"]]},{"id":"bb454826.c1adf8","type":"function","z":"c33678df.aad368","name":"","func":"//var go = msg.payload;\n\nif ( msg.payload == 1 ){\n   var screen = \" \";\n}\n    \nvar msg = {screen: screen};\n\nreturn msg;","outputs":"1","noerr":0,"x":699,"y":990,"wires":[["c933afe7.baec58","8f7672f9.b15088"]]},{"id":"1cdb430d.21bd75","type":"ui_group","z":"","name":"Default","tab":"360388d6.389838","disp":true,"width":"6"},{"id":"360388d6.389838","type":"ui_tab","z":"","name":"testing","icon":"dashboard"}]



Op woensdag 8 februari 2017 12:35:59 UTC+1 schreef esc...@missionroom.nl:
Reply all
Reply to author
Forward
0 new messages