I have a duration in minutes. I want to use that number to print out a “time” showing hours and minutes with two digits for each (e.g. 2 minutes would become “00:02”; 110 minutes would become “01:50”).
I’ve been looking over the supported Date, Math and String functions and can’t wrap my head around how I might get this to work. Any suggestions?
Hi,
I am trying to implement on my prototype a stopwatch in a format 00(hr):00(min):00(sec). When start on page load it is supposed to start counting seconds and minutes and stop counting when stop button is hit.
I’ve tried to access these links for help but they have been archived.
(NOTE: thesliceand'x0'is to make sure there is a leading0if any of the units are a single digit number)
To count you just have to add 1 to your variable ever second. I would do this by adding a hidden widget and using an event like Move to add to the variable. Add a condition to check a different variable which will be toggled by your button so you can control whether the timer should stop or not. Then you have a separate case that fires the event again after 1 second. This is your timer.
OnMove If variable StopCounter does not equal true
Set variable Timer to: [[Timer + 1]]
If true
Wait 1000ms
Fire event this widget Move
Then to top it all off you just need to make your button set StopCounter to “true” and the timer will stop. If you then change StopCounter to be “false” or nothing then it will start counting again.
Not sure what you mean about the minutes. If you’re storing your timer as seconds the expression I have works fine. 5 minutes 30 seconds is 330 seconds. 330 divided by 60 is 5.5, the floor of which is 5, as expected.
Assuming a Timer value of 330: Math.floor(330/60) == 5 Math.floor((330-Math.floor(330 / 3600)*3600) / 60) == 5
You can test it yourself in the browser console.
The result for low numbers are the same. But in the expression [hr]:[min]:[sec] the minutes were able to raise till 99. This means for example if you enter the value of seconds for 90 minutes you get the result 01:90:00. Therefore, I substracted the amount of seconds used for the hours. Maybe it is not simplest and cleanest way, but it did the job. I don’t fully understand “Timer%60” eather but your “x0”…".slice(-2)" made my day. Thank you