Skip to content

Instantly share code, notes, and snippets.

@Benzeliden
Created December 10, 2017 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Benzeliden/aa5bab4ba8ae84e066c0b2c5164ba179 to your computer and use it in GitHub Desktop.
Save Benzeliden/aa5bab4ba8ae84e066c0b2c5164ba179 to your computer and use it in GitHub Desktop.
local text = display.newText({
text = "Initial text",
x = display.contentCenterX,
y = display.contentCenterY,
})
transition.to(text, {
y = display.contentCenterY * 0.2,
time = 5000,
iterations = -1,
transition = easing.continuousLoop,
})
local doGood = true
local bigNumber = 122033000
if (doGood) then
--------------------------------------
--Good coroutine (no freeze)
--------------------------------------
local function coroutineFunc(maxI)
local n, i = 1, 2
while (i < maxI) do
local steps = math.min(1000000, maxI - i + 1)
for j = 1, steps do
n = n * i / (i - 1)
i = i + 1
end
coroutine.yield(i)
end
return n
end
local co = coroutine.create(coroutineFunc)
local coStatus, newRes
timer.performWithDelay(50, function(event)
if coroutine.status(co) ~= "dead" then
coStatus, newRes = coroutine.resume(co, bigNumber)
print("resume", coStatus, newRes)
text.text = "Processing " .. newRes
else
timer.cancel(event.source)
print("cancel")
text.text = text.text .. " Done!"
end
end, -1)
else
--------------------------------------
--Bad coroutine (freeze)
--------------------------------------
local function coroutineBadFunc(maxI)
local n = 1
for i = 2, maxI do
n = n * i / (i - 1)
end
return n
end
timer.performWithDelay(100, function()
local co = coroutine.create(coroutineBadFunc)
local coStatus, result = coroutine.resume(co, bigNumber)
text.text = "Processing " .. result .. " Done!"
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment