5

In an SCXML state machine, how can I say "Fire an event 3 minutes after I enter this state, but not if I sit in the state for 2.9 minutes and then leave. If I re-enter the state, restart the timer (don't go off in 0.1 minutes)"

Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • +1 Informative. What platform do you use for SCXML? – masoud Feb 25 '13 at 23:08
  • @MM. I'm currently using [my own interpreter](https://github.com/Phrogz/LXSC) which runs on Lua on [custom hardware](http://shield.nvidia.com/). (This Lua-based interpreter may be replaced by a custom C++ one for speed in the near future.) – Phrogz Feb 25 '13 at 23:19

1 Answers1

6

Use <send> to fire a delayed event (with any name, e.g. "timeout") when you enter the state, and use <cancel> when you exit the state to remove the timer. You must make sure that you create a unique ID for each <send> instance that you plan to later cancel.

<scxml xmlns='http://www.w3.org/2005/07/scxml' version='1.0'>
  <state id="s1">
    <onentry><send id="state1-timer" event="timeout" delay="180s"/></onentry>
    <onexit><cancel sendid="state1-timer"/></onexit>
  </state>
  <!-- ... --->
</scxml>

Note: you can only use either s (seconds) or ms (milliseconds) for the delay duration, per the CSS2 time spec. Thus, 3 minutes is 180s.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • 2
    Just a note for future users ... the answer above is close but not exact. See http://www.w3.org/TR/2006/WD-scxml-20060124/ in the send, change id to sendid. It will send just fine, but that sets the ID for the cancel - it will not cancel correctly. I didn't realize this until I was walking the scheduler code and it wasn't triggering! – Evan Reynolds Oct 03 '13 at 17:58
  • 2
    @EvanReynolds Your comment is based on an old working draft of the SCXML spec. Per the [candidate recommendation](https://www.w3.org/TR/scxml/#send), the `` element has an `id` attribute, not a `sendid` attribute. (And, the `` element [uses a `sendid` attribute](https://www.w3.org/TR/scxml/#cancel).) – Phrogz Jun 14 '17 at 20:41
  • 1
    It was current when I wrote the comment. :-) Thank you for the updated information, that is good to know! – Evan Reynolds Jun 20 '17 at 00:04