918af4f3ac
* change simulator svg * change radio image * Remove google fonts cdn * change color of 'advanced' button * font fix * font fix 2 * display fix * change fullsceen simulator bg * Continuous servo * handle continuous state * adding shims * update rendering for continuous servos * fixing sim * fix sig * typo * fix sim * bump pxt * bump pxt * rerun travis * Input blocks revision - add Button and Pin event types - merge onPinPressed & onPinReleased in new onPinEvent function - create new onButtonEvent function * update input blocks in docs and tests * remove device_pin_release block * Hide DAL.x behind Enum * bring back deprecated blocks, but hide them * shims and locales files * fix input.input. typing * remove buildpr * bump V3 * update simulator aspect ratio * add Loudness Block * revoke loudness block * Adds soundLevel To be replaced by pxt-common-packages when DAL is updated. * Remove P0 & P3 from AnalogPin Co-authored-by: Juri <gitkraken@juriwolf.de>
67 lines
2.1 KiB
Markdown
67 lines
2.1 KiB
Markdown
# wait For Event
|
|
|
|
Stop running the current code and wait for an event.
|
|
|
|
```sig
|
|
control.waitForEvent(0, 0)
|
|
```
|
|
You might want your code to stop for a while until some event you're expecting happens.
|
|
If you want your code to pause until a known event happens, use a ``||control:wait for event||`` block.
|
|
A known event is something you identify that happens on the board or in your program.
|
|
An event has both a source and a cause. The source is where the event comes from, like a sensor or
|
|
some condition in your program. The cause is what made the event happen at the source.
|
|
|
|
You assign numbers for your event sources and causes. These numbers are used by [``||control:raise event||``](/reference/control/raise-event) to announce to your program that an event just happened.
|
|
|
|
As an example, you could make a timer that always causes an event every 100 milliseconds. The source
|
|
number for the timer is `6` and the cause value is `1`. The program could wait for one of the time
|
|
events like this:
|
|
|
|
```blocks
|
|
const myTimer = 6;
|
|
const timerTimeout = 1;
|
|
|
|
control.runInParallel(function() {
|
|
while (true) {
|
|
control.waitMicros(100000)
|
|
control.raiseEvent(myTimer, timerTimeout)
|
|
}
|
|
})
|
|
|
|
control.waitForEvent(myTimer, timerTimeout)
|
|
```
|
|
|
|
## Parameters
|
|
|
|
* **id**: the identification [number](/types/number) (the source) of this event, such as: `10`.
|
|
* **value**: a [number](/types/number) tells what the cause of the event is, like: `4`.
|
|
|
|
## Example #example
|
|
|
|
Make a timeout timer to signal every 2 seconds. Wait two times and write to the
|
|
console each time.
|
|
|
|
```blocks
|
|
const myTimer = 6;
|
|
const timerTimeout = 1;
|
|
|
|
control.runInParallel(function() {
|
|
while (true) {
|
|
pause(2000)
|
|
control.raiseEvent(myTimer, timerTimeout)
|
|
}
|
|
})
|
|
|
|
control.waitForEvent(myTimer, timerTimeout)
|
|
console.log("Timer timeout")
|
|
control.waitForEvent(myTimer, timerTimeout)
|
|
console.log("Timer timeout")
|
|
```
|
|
|
|
**This is an advanced API.** For more information, see the
|
|
[@boardname@ runtime messageBus documentation](https://lancaster-university.github.io/microbit-docs/ubit/messageBus/).
|
|
|
|
|
|
## See also #seealso
|
|
|
|
[raise event](/reference/control/raise-event), [on event](/reference/control/on-event) |