-
-
Save Benzeliden/aa5bab4ba8ae84e066c0b2c5164ba179 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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