Jump to content

Countdown timer


Karuzo

Recommended Posts

There are other ways to do it, but this is one method:

function countdownFin() 
    --whatever you want to happen here 
end 
  
local countdown = setTimer(countdownFin, 600000, 1) --set the timer for 10 minutes 
  
setTimer( 
function() 
    local x = getTimerDetails(countdown) 
    local timeLeft = math.ceil(x/1000) 
    outputChatBox(convertSecondsToMinutes(timeLeft)) 
end, 1000, 0) 
  
function convertSecondsToMinutes(sec) --turn the seconds into a MM:SS format 
    local temp = sec/60 
    local temp2 = (math.floor(temp)) --this equals the minutes 
    local temp3 = sec-(temp2*60) --and this is seconds 
    if string.len(temp3) < 2 then --make sure it's displayed correctly (MM:SS) 
        temp3 = "0"..tostring(temp3) 
    end 
    return tostring(temp2)..":"..tostring(temp3) 
end 

EDIT: Just quickly tested it and fixed a tiny bug. The code works now.

Link to comment
  • 6 years later...
On 15/03/2014 at 17:46, Driggero said:

There are other ways to do it, but this is one method:


function countdownFin() 
    --whatever you want to happen here 
end 
  
local countdown = setTimer(countdownFin, 600000, 1) --set the timer for 10 minutes 
  
setTimer( 
function() 
    local x = getTimerDetails(countdown) 
    local timeLeft = math.ceil(x/1000) 
    outputChatBox(convertSecondsToMinutes(timeLeft)) 
end, 1000, 0) 
  
function convertSecondsToMinutes(sec) --turn the seconds into a MM:SS format 
    local temp = sec/60 
    local temp2 = (math.floor(temp)) --this equals the minutes 
    local temp3 = sec-(temp2*60) --and this is seconds 
    if string.len(temp3) < 2 then --make sure it's displayed correctly (MM:SS) 
        temp3 = "0"..tostring(temp3) 
    end 
    return tostring(temp2)..":"..tostring(temp3) 
end 
 

 

EDIT: Just quickly tested it and fixed a tiny bug. The code works now.

Thank you for providing this code

i have upgraded it to be dx text rather than output chat box

here is the code:

function countdownFin() 
    --whatever you want to happen here 
end 
  
local countdown = setTimer(countdownFin, 600000, 1) --set the timer for 10 minutes 

setTimer( 
    function() 
        removeEventHandler ( "onClientRender", root, drawText)
        local x = getTimerDetails(countdown) 
        local timeLeft = math.ceil(x/1000) 
        function drawText()
            dxDrawText(convertSecondsToMinutes(timeLeft), 500, 500)
        end
        addEventHandler ( "onClientRender", root, drawText)
    end
, 1000, 0) 
  
function convertSecondsToMinutes(sec) --turn the seconds into a MM:SS format 
    local temp = sec/60 
    local temp2 = (math.floor(temp)) --this equals the minutes 
    local temp3 = sec-(temp2*60) --and this is seconds 
    if string.len(temp3) < 2 then --make sure it's displayed correctly (MM:SS) 
        temp3 = "0"..tostring(temp3) 
    end 
    return tostring(temp2)..":"..tostring(temp3) 
end 

 

Link to comment

fixed some debug warnings and errors:

function countdownFin() 
    --whatever you want to happen here 
end 
timerDirection = 10000 -- 10 seconds
countdown = setTimer(countdownFin, timerDirection, 1)
countDownTimes = (timerDirection / 1000) -- Convert timerDirection into seconds to prevent debug warnings by setTimer function

setTimer( 
    function() 
        if (isTimer(countdown) == true) then
            removeEventHandler ( "onClientRender", root, drawText)
            x = getTimerDetails(countdown) 
            timeLeft = math.ceil(x/1000) 
            function drawText()
                dxDrawText(convertSecondsToMinutes(timeLeft), 500, 500)
            end
            addEventHandler ( "onClientRender", root, drawText)
        else
            removeEventHandler ( "onClientRender", root, drawText)
        end
    end
, 1000, countDownTimes) 

function convertSecondsToMinutes(sec) --turn the seconds into a MM:SS format 
    local temp = sec/60 
    local temp2 = (math.floor(temp)) --this equals the minutes 
    local temp3 = sec-(temp2*60) --and this is seconds 
    if string.len(temp3) < 2 then --make sure it's displayed correctly (MM:SS) 
        temp3 = "0"..tostring(temp3) 
    end 
    return tostring(temp2)..":"..tostring(temp3) 
end 

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...