-
Notifications
You must be signed in to change notification settings - Fork 22
Create timers
The "Timers" category contains blocks that allow you to do something at some specific time.
There are two kind of timers supported by those blocks:
- A timeout block allows to execute the included blocks once, after some specified time delay.
- An interval block allows to execute the included blocks periodically, with a specified time interval in between the executions.
Both timers will be started as soon as a new input message arrives.
There are also two Stop... blocks available:
- A stop timeout block allows to stop a specified timeout block, which is currently active.
- A stop interval block allows to stop a specified interval block, which is currently active.
Both stop-blocks will be used to stop a currently active timer. Otherwise an extra new second timer would be started, while the current active timer is still running. This would cause indesirable effects, caused by multiple timers running in parallel (each executing their included blocks).
Note that the interval block will automatically stop (its previous timer) when a new interval timer is started. So no need to use a stop interval here, unless you want to stop an interval timer IF certain conditions are met:
Some remarks:
- In order to be able to stop an active timer, each timer needs to have its own unique name. That is the only way to stop an active timer, by specifying the name of the timer that should be stopped! The Stop-blocks will show a dropdown of all corresponding available timers in the workspace.
- In the generated code, the timer instances will be stored on context memory. This is the only way to make sure the timers can be cleared afterwards, when the next input message arrives or in the close event.
The following example flow shows how to use both types of timers:
[{"id":"6f9044a0.b3870c","type":"Blockly","z":"c2a7925b.6e143","func":"if (context.get(\"timeout_1\")) {\n clearTimeout(context.get(\"timeout_1\"));\n context.set(\"timeout_1\", undefined);\n}\nvar timeout_1 = setTimeout(function() {\n node.send([msg]);\n}, 3000);\ncontext.set(\"timeout_1\", timeout_1);","workspaceXml":"<xml xmlns=\"https://developers.google.com/blockly/xml\">\n <block type=\"clear_timeout\" id=\"Q83YfytwSD~%X8Vn832C\" x=\"-187\" y=\"-162\">\n <mutation xmlns=\"http://www.w3.org/1999/xhtml\" dropdown_options=\"{"nothing_selected":"none",";qWY9%iIXdYS{7I@S1s0":"timeout_1"}\"></mutation>\n <field name=\"NAME\">;qWY9%iIXdYS{7I@S1s0</field>\n <next>\n <block type=\"set_timeout\" id=\";qWY9%iIXdYS{7I@S1s0\">\n <field name=\"NAME\">timeout_1</field>\n <field name=\"DELAY\">3</field>\n <field name=\"UNIT\">sec</field>\n <statement name=\"STATEMENT\">\n <block type=\"node_send\" id=\"(GgJ]u-3f|Nf^?KS,u:S\">\n <field name=\"OUTPUT_NR\">1</field>\n <value name=\"MESSAGE_INPUT\">\n <shadow type=\"node_msg\" id=\"O=fXK^rqJuO|7R~-feV)\"></shadow>\n </value>\n </block>\n </statement>\n </block>\n </next>\n </block>\n</xml>","outputs":1,"blocklyConfig":"d7a036fa.2c0298","name":"","x":460,"y":520,"wires":[["23f77ae.7ac7086"]]},{"id":"bb11da23.5f7ff8","type":"inject","z":"c2a7925b.6e143","name":"Delay this msg 3 seconds","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Delayed with 3 seconds","payloadType":"str","x":210,"y":520,"wires":[["6f9044a0.b3870c"]]},{"id":"23f77ae.7ac7086","type":"debug","z":"c2a7925b.6e143","name":"Delayed msg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":520,"wires":[]},{"id":"abafc1856f0580f5","type":"Blockly","z":"c2a7925b.6e143","func":"if (context.get(\"interval_1\")) {\n clearInterval(context.get(\"interval_1\"));\n context.set(\"interval_1\", undefined);\n}\nvar interval_1 = setInterval(function() {\n node.send([msg]);\n}, 2000);\ncontext.set(\"interval_1\", interval_1);","workspaceXml":"<xml xmlns=\"https://developers.google.com/blockly/xml\">\n <block type=\"clear_interval\" id=\"lxEB}pT[]h;|XRLCw;MH\" x=\"-237\" y=\"-287\">\n <mutation xmlns=\"http://www.w3.org/1999/xhtml\" dropdown_options=\"{"nothing_selected":"none","9x|||hb7Dv071Rpxc8t(":"interval_1"}\"></mutation>\n <field name=\"NAME\">9x|||hb7Dv071Rpxc8t(</field>\n <next>\n <block type=\"set_interval\" id=\"9x|||hb7Dv071Rpxc8t(\">\n <field name=\"NAME\">interval_1</field>\n <field name=\"INTERVAL\">2</field>\n <field name=\"UNIT\">sec</field>\n <statement name=\"STATEMENT\">\n <block type=\"node_send\" id=\"Xqj)7E{pqs!}R4^ad1B*\">\n <field name=\"OUTPUT_NR\">1</field>\n <value name=\"MESSAGE_INPUT\">\n <shadow type=\"node_msg\" id=\"WRdFmo@ZPjzy4o|Hbmm*\"></shadow>\n </value>\n </block>\n </statement>\n </block>\n </next>\n </block>\n</xml>","outputs":1,"blocklyConfig":"d7a036fa.2c0298","name":"","x":460,"y":580,"wires":[["b6d7c80b9cd67967"]]},{"id":"3d2d2c395dc5c3d8","type":"inject","z":"c2a7925b.6e143","name":"Repeat this msg every 2 seconds","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Resend every 2 seconds","payloadType":"str","x":230,"y":580,"wires":[["abafc1856f0580f5"]]},{"id":"b6d7c80b9cd67967","type":"debug","z":"c2a7925b.6e143","name":"Repeated msg","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":580,"wires":[]},{"id":"d7a036fa.2c0298","type":"blockly-config","showTrashcan":true,"allowComments":true,"showZoomControl":true,"toolboxPosition":"left","renderer":"geras","name":"Left"}]
And a demo to show how timeout blocks can be used: