If you don't mind use a lib, you may check the example for
Continue() in the
Scorpio - asynchronous framework, the async frame work is designed to smooth the operation of codes and make the fps stable.
The async framework is also designed based on the debugprofilestop. The lib can be used as an API lib, works like :
Lua Code:
Scorpio.Continue(function()
-- The function is processed within in a coroutine
-- so, async API can be used
local testTable = {}
for i = 1, 100000 do
testTable[i] = i
end
local startTime = GetTime()
for k, v in pairs(testTable) do
-- The Continue API will yield the current coroutine
-- And then check if there is still enough time to resume it
-- So the fps can be protected
Scorpio.Continue()
-- If the process is yield, it'll be resumed in the next phase(with OnUpdate)
-- So we can check how many the recycle processed in one phase
if GetTime() ~= startTime then
print(k)
startTime = GetTime()
end
end
end)
I prefer the Lua's coroutine compares to the callback since the logic can be put together