Interrupting a WHILE or FOR loop

Using a WHILE loop will suspend any other processing by the Nextion, unless you have DOEVENTS added into the loop. That only refreshes the screen.

I have a countdown timer onscreen and want to be able to issue a STOP command from a screen press, but the Nextion doesn’t recognize it.

How could I make that work?

Can’t you just disable the timer as follows:
which should stop the countdown time

I don’t believe it’s possible to interrupt a loop with a touch event. I ‘want’ to be able to touch a ‘STOP’ button on the HMI to stop the countdown timer which is displaying on screen.

Not sure if this helps but google ‘nextion while’ and look at youtube video ‘148 nextion’

That video explains the loops, but not much help for what I need. I then discovered the ‘timer’ object and I think that will work perfectly!

In your WHILE/FOR loop, check the tch0 and tch1 values. These indicate the current touch positions (x,y coords). If they indicate a valid touch location then “STOP”

Didn’t know about those, I’ll check it out. Thanks!!

Use break or continue to interrupt the loop.

I’d just like to confirm that using tchX worked perfectly for me.

So a big word of thanks to you for that suggestion as I’d been battling to find a way to exit a while loop when the button was released, but had given up thinking it was not possible with a Nextion.

I wanted a button press to repeatedly send a serial byte every so many milliseconds while the button is pressed but stop sending the byte (i.e. end the while() loop) as soon as I released the button.
I made this code:

printh 09

And it worked perfectly. So once again thanks very much.


Timers are this what is dedicated for such things.
You can put this code into timer object ex. tm0.
Make sure that tm0.en=0 and tm0.tim=50 so it is not runnign if button is not pressed.
By pressing button You may activate this timer to mak it running.
Button touch press code:
Button toch release code

Timer tm0 code:
printh 09

This approch doesn’t block other tasks runnign in the background.

Thanks very much Bob.

I’d heard about timers to do this but didn’t know how they worked. Just tried your instructions and it worked perfectly.