Skip to content

TimerDemo.llua

xu.jingyu edited this page Aug 11, 2020 · 1 revision

TimerDemo.lua

copy下面代码,运行试试

local timer = Timer()
timer:interval(1)
timer:repeatCount(100)

local count = 0
Color_White = Color(255, 255, 255, 1)
Color_Gray = Color(105, 105, 105, 1)

local label1 = Label()
label1:width(200):height(50):marginLeft(10):marginTop(20)
label1:text("start - 定时器开始")
label1:bgColor(Color_Gray):textColor(Color_White)
window:addView(label1)
label1:onClick(function()
    timer:start(function()
        print(count)
        count = count+1
    end)
end)

local label2 = Label()
label2:width(200):height(50):marginLeft(10):marginTop(80)
label2:text("pause - 暂停")
label2:bgColor(Color_Gray):textColor(Color_White)
window:addView(label2)
label2:onClick(function()
    print("暂停定时器")
    timer:pause()
end)

local label3 = Label()
label3:width(200):height(50):marginLeft(10):marginTop(140)
label3:text("resume - 恢复")
label3:bgColor(Color_Gray):textColor(Color_White)
window:addView(label3)
label3:onClick(function()
    print("恢复定时器")
    timer:resume()
end)

local label4 = Label()
label4:width(200):height(50):marginLeft(10):marginTop(200)
label4:text("stop - 停止")
label4:bgColor(Color_Gray):textColor(Color_White)
window:addView(label4)
label4:onClick(function()
    print("停止定时器,并且重置count=0")
    count = 0
    timer:stop()
end)
Clone this wiki locally