How do you update a label every second?

I have a code block, shown below;

I want to update the Duration.text every second. How can I achieve it?

Using Clock Component

1 Like

Use a Clock component ans put your blocks into the Clock.Timer event
http://ai2.appinventor.mit.edu.ezproxy.canberra.edu.au/reference/components/sensors.html#Clock

A very good way to learn App Inventor is to read the free Inventor's Manual here in the AI2 free online eBook App Inventor 2 Book: Create Your Own Android Apps ... the links are at the bottom of the Web page. The book 'teaches' users how to program with AI2 blocks.
There is a free programming course here Course In A Box and the aia files for the projects in the book are here: App Inventor 2 Book: Create Your Own Android Apps
How to do a lot of basic things with App Inventor are described here: How do you...? .

Also do the tutorials Tutorials for MIT App Inventor to learn the basics of App Inventor, then try something and follow the Top 5 Tips: How to learn App Inventor

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

2 Likes

I have already using a timer block in the app and I have tried to put the Duration.text block in clock.timer it is not updating every after second. What am I doing wrong here?

This code updates the Duration.text only when timer ends.

Maybe this call/update section is consuming more portion of 1000 millis,

image

What this section is doing.

To set timer enabled to false stops the clock...

Also remove all irrelevant blocks...
Only ask for permission once...

Taifun

Point1 - Timer interval must be 1000ms inorder to get value for every seconds..

Point2 - And you should not set clock timer enabled to false in when clock timers event block

Thanks for helping me with the issue. I have got the label update as I needed by following the suggested changes. Complete code blocks are below;

blocks
This block initializes a variable


This block calculates the duration between the present time and the time picked (always in the future). It stores the duration in a variable and also displays it in a label that updates after every second.


When the picked time is equal to the present time the timer should stop. I tried to achieve it by using while.test block but when I use this while.test block the app becomes unresponsive and crashes.

the duration is positive in your case, isn't it?
you should put an if statement into the Clock.Timer event like this

if duration < 0
then do something

Taifun

I have followed the suggestion but the app crashes after picking time in the time picker.

Here is how you can work with Seconds, as you have already converted millis to seconds,

OR, this could also be, to work with Milli-Seconds,

P.S. If we are not having any regrence time, I think we can go with average days in months, and calculate months and years from given seconds, i.e., 30.437