* update yotta defaults for 16kb devices * refactor deprecated blocks * updates for button events * update button events * update refference * update docs * update docs * update button event blocks * update docs * update block id
1.6 KiB
every Interval
Run part of the program in a loop continuously at a time interval.
loops.everyInterval(500, function () {})
If you want to run some code continuously, but on a time interval, then use an every loop. You set the amount of time that the loop waits before the code inside runs again. This is similar to a forever loop, in that it runs continuously, except that there's a time interval set to wait on before the loop runs the next time. This loop is useful when you want some of a program's code run on a schedule.
Parameters
- interval: a number that is the amount of time in milliseconds to wait before running the loop again.
~ reminder
Event-based loops
Both the every loop and the forever loop are event-based loops where the code inside is run as part of a function. These are different from the for and while loops. Those are loops are part of the programming language and can have break and continue statements in them. You can NOT use break or continue in either an every loop or a forever loop.
~
Example
At every 200
milliseconds of time, check if either the A or B button is pressed. If so, show on the screen which one is pressed.
loops.everyInterval(200, function () {
if (input.buttonIsPressed(Button.A)) {
basic.showString("A")
} else if (input.buttonIsPressed(Button.B)) {
basic.showString("B")
} else {
basic.clearScreen()
}
})