2014-02-26 70 views
2

循環中調用計時器要調用一個計時器無限次我使用在科羅娜SDK

function A() 
    print("Hello") 
    timer.performWithDelay(100, B, 1) 
end 

function B() 
    timer.performWithDelay(500, A, 1) 
end 

timer.performWithDelay(100, A, 1) 

這樣,如果我想打印招呼在特定的時間間隔,我與這兩個功能調整它。 但是我面臨的問題是一段時間後計時器變慢,並且非常迅速地調用函數A. 任何人都可以建議我如果我這樣做的權利?並解決計時器問題,我應該怎麼做?

在此先感謝。

回答

4

如果你想要把一個定時器無限的時間,你可以使用:

timer.performWithDelay(100, functionName, -1) 

timer.performWithDelay(100, functionName, 0) 

在你的情況,你需要再次調用之前取消計時器。所以,不喜歡如下:

local timer_1,timer_2,timer_3 
function A() 
    print("Hello") 
    if(timer_1)then timer.cancel(timer_1) end -- cancelling 'timer_1' if exists 
    if(timer_3)then timer.cancel(timer_3) end -- cancelling 'timer_3' if exists 
    timer_2 = timer.performWithDelay(100, B, 1) 
end 

function B() 
    if(timer_2)then timer.cancel(timer_2) end -- cancelling 'timer_2' if exists 
    timer_3 = timer.performWithDelay(500, A, 1) 
end 

timer_1 = timer.performWithDelay(100, A, 1) 

在這裏你可以看到,我已經創建計時器對象(TIMER_1,定時器_2和timer_3),並取消這可能是正在進行的定時器,調用另一個前/同一個定時器。

保留編碼............. :)

+0

謝謝。我試過這個,但仍然給出了同樣的問題。 –

+0

@ Akshada-Systematix:在下一次調用之前,請嘗試取消所有其他定時器。這將做到這一點。 –