pxt-calliope/docs/reference/control/in-background.md
Juri Wolf 77ed2ccfb1
V4 updates (#210)
* update pxt.json files

* Fix button event enums

fixes https://github.com/microsoft/pxt-calliope/issues/206

* Fix Safari CSS Rule for iOS app

fixes https://github.com/microsoft/pxt-calliope/issues/205

* aprove preffered repos

should fix https://github.com/microsoft/pxt-calliope/issues/167
2023-01-11 09:51:27 -08:00

1.0 KiB

Run In Background

Run part of a program while the rest of it is doing something else.

control.inBackground(() => {
})

~hint

For more information, read The @boardname@ - a reactive system. It is pretty advanced!

~

Example

This program shows how running in the background can say what is stored in a variable like num, while another part (on button pressed) changes what is stored there.

let num = 0
control.inBackground(() => {
    while (true) {
        basic.showNumber(num)
        basic.pause(100)
    }
})
input.onButtonEvent(Button.A, input.buttonEventValue(ButtonEvent.Down), () => {
    num++;
})

This program does the same thing, but in a more usual way, with a forever loop.

let num = 0
basic.forever(() => {
    basic.showNumber(num)
})
input.onButtonEvent(Button.A, input.buttonEventValue(ButtonEvent.Down), () => {
    num++;
})

See also

while, forever, on button pressed