FIY9AL Posted April 9, 2020 Share Posted April 9, 2020 I have created countdown functions based on this thread: and also this thread: my countdown function are useful and you can use it evreywhere Example 1: Here i make 'nil' values displayCountDown(nil, nil, 5, "%02s : %02s : %02s") to create a dx draw text like this: the first argument is to till hours, second is minutes, third is seconds, fourth is to create format and if you set some arguments to 'nil' it will be zeros Standard: displayCountDown(hh, mm, ss, format) Lets see more examples: Example 2: displayCountDown(24, 50, 30, "%02s : %02s : %02s") Example 3: displayCountDown(24, nil, 30, "%02s : %02s : %02s") Example 4: Here i have changed to format to [hh : mm] displayCountDown(24, nil, 30, "%02s : %02s") Example 5: Here i hve changed the format digits to [1] per item displayCountDown(1, 1, 9, "%01s : %01s : %01s") And this is the function code, you can export it or put it in your own code function displayCountDown(hh, mm, ss, format) local function convertSecondsToMinutes(ms) local hours = math.floor (ms/3600000) -- convert ms to hours local mins = math.floor (ms/60000) -- convert ms to mins local secs = math.floor ((ms/1000) % 60) -- convert ms to secs return string.format ( format, hours, ( mins - hours * 60 ), secs ) -- print hh:mm:ss format -- "%02s : %02s : %02s" is the default format end local function countdownFin() -- to do after countdown finished end direction = 0 if (hh ~= nil) then if (hh <= 23) and ((hh >= 0)) then hh = hh * 3600000 direction = direction + hh else outputChatBox("[ERROR] Hours must be between 0 and 24",255,0,0) direction = nil end end if (direction ~= nil) then if (mm ~= nil) then if (mm <= 59) and ((mm >= 0)) then mm = mm * 60000 direction = direction + mm else outputChatBox("[ERROR] Minutes must be between 0 and 60",255,0,0) direction = nil end end end if (direction ~= nil) then if (ss ~= nil) then if (ss <= 60) and ((ss >= 0)) then ss = ss * 1000 direction = direction + ss else outputChatBox("[ERROR] Seconds must be between 0 and 60",255,0,0) direction = nil end end end if (direction ~= nil) then countDownTimes = (direction / 1000) -- Convert direction into seconds to prevent debug warnings by setTimer function countdown = setTimer(countdownFin, direction+1000, 1) end local function drawText(text) local function drawTextZG() dxDrawText(text, 500, 500) -- dx draw on screen end addEventHandler ( "onClientPreRender", root, drawTextZG) -- draw evrey screen render setTimer( function() removeEventHandler ( "onClientPreRender", root, drawTextZG) -- remove text after each second end , 1000, 1) -- do this function evrey second once end if (direction ~= nil) then setTimer( function() drawText(convertSecondsToMinutes(getTimerDetails(countdown))) -- sends hh:mm:ss foramt to dx draw function end , 1000, countDownTimes-1) end end displayCountDown(23, 59, 60, "%02s : %02s : %02s") You can be creative with it you can use very diffrent formats like hh : mm : ss, hh, hh:mm, anything you can make it countdown any number you want you can upgrade it to be getting value from gui window you can change the draw text color, font,... to match your needs i hope this function helpful for evreyone i want to see your works done with this function Thank you for your time reading my thread. Link to comment
Moderators IIYAMA Posted April 9, 2020 Moderators Share Posted April 9, 2020 @FIY9AL You could also add: function displayCountDown(hh, mm, ss, format, countdownFin) if countdownFin then countdown = setTimer(countdownFin, direction+1000, 1) end Else there the callback function is very static. 2 1 Link to comment
Recommended Posts