Trying to make a timer that repeats. Really really confused and stuck

Okay, for context, I am quite new to both Solar2d/Corona and the Lua programming language, I do have decent experience in C, C++, Java, and Python. I’m currently making an exercise app. As part of that app, I was hoping to have a timer for each exercise. The timer’s time would reset after each exercise is done to go the next one. I have been trying to get this to work for a ridiculously long amount of time. I have tried countless things, and am kind of desperate at this point for help or anything that could help point me in the right direction. Here is the code (excluding parts which aren’t relevant) and an explanation for the code below it:

local composer = require( "composer" )

local scene = composer.newScene()

local background

local function sleep(s)
	local ntime = os.clock() + s/10
	repeat until os.clock() > ntime

local function updateTime(secondsLeft, clockText, it)
	while (secondsLeft >= 0) do
		secondsLeft = secondsLeft - 1
                clockText.text = secondsLeft
	if (it ~= 0) then

function scene:create( event )
	local sceneGroup = self.view
	local para = event.params.var1
	it = event.params.var2
	local texty = event.params.var3
	background = display.newImageRect(sceneGroup,"images/whiteBackground.jpg",768,1500)
	background.x = display.contentCenterX
	background.y = display.contentCenterY
	local secondsLeft = 20
	local it = 3
	local clockText = display.newText( secondsLeft, display.contentCenterX, 80, native.systemFont, 72 )
	clockText:setFillColor( 0.7, 0.7, 1 )

	local nextButton = display.newText(sceneGroup, "Regular", 500, 1300, nativeSystemFont, 60)
	nextButton:addEventListener("tap", function ()
	updateTime(secondsLeft, clockText, it)


return scene

I start off by defining a text object, clockText. This is to display how many seconds are left, acting as the visual representation of the counter. I have a button that starts the process by calling the updateTime function. Three variables are passed to the updateTime function: secondsLeft, the number of seconds that the timer will run for, clockText, the text object that will display the time left, it, the number of iterations the timer will run for (the number of exercises that will be ran).

In the updateTime function itself, there is a while loop that subtracts one from the secondsLeft and updates the text of the clockText to be equal to the new value of secondsLeft. As well, there is a call to a sleep function, which makes the program wait for k seconds, in this case one. After the while loop is finished, an if statement checks if it (the number of iterations left) is 0. If not, the function is called again with it subtracted by 1.

Currently, the text of clockText updates when I print it to the console, but the new text doesn’t show up on the actual app’s screen. Only when the function call has ended does the text of clockText update on the app itself. The best I’ve gotten is for the text to update on the screen for one iteration, but break when I try to get it to do more than one in a row. And I don’t even remember how I got that. If any of this sounds confusing at all, then sorry I just have been working on this for quite a while and haven’t gotten much sleep, I’d be happy to clarify anything. Any and all help is very appreciated.

I noticed this post but am about to take off. I’ll check on it tonight or tomorrow.

That said, I didn’t see a summary of your goal/purpose (could have missed). Could you bullet list your goal(s) here?

Note: The post is great, I just want to be sure when I give this a thorough looking at, that I know exactly what you want to do.

1 Like

This is what timers are meant to do.

I think I saw you posting some earlier Stack Overflow posts, but you were also trying to merge the timers with loops? If so, that’s fighting against how Solar works: the timer will not happen immediately, but its block of code will fire each time the delay expires.

Instead of updateTime, try something like this after you make clockText (untested):

local start = system.getTimer()

timer.performWithDelay(50, function(event)
  local lapse = event.time - start
  if lapse >= 20 * 1000 then -- 20 seconds are up?
    clockText.text = 20 - lapse -- you can make this prettier by rounding, etc.
end, 0) -- iterate indefinitely (in 50 ms increments or so)
1 Like

No problem:

  1. Have a countdown timer that counts down
  2. Have the countdown timer be represented by a text object on screen
  3. The countdown timer must repeat a certain number of times (like when it reaches 0 it starts again)

The current code that I presented does all of those things except 2. The text object doesn’t update when the number of seconds remaining changes

Yeah, I was on stack overflow without much success. I think I stated my questions very poorly there lol.

Thanks a lot for the advice and code! I’ll make some adjustments with how I want the timer to work, then report back.

It works perfectly. I set it so it will repeat a certain number of times. Thanks again!

1 Like

Thanks for answering!

1 Like