TI-Nspire & Lua / タイマーの tick するたびに経過時間を出力する / グローバル変数に頼らない / コルーチンを使ってみる

Every time a timer ticks, the following script will return the elapsed time.
As a trial, a co-routine has been used.


.lua

function elapsedTime()
   local initTime = timer.getMilliSecCounter()
   for i = 1, math.huge do
      coroutine.yield((timer.getMilliSecCounter() - initTime) / 1000, i)
   end
end
local elapsedTime_co = coroutine.wrap(elapsedTime)

-------------------------------------------------
-------------------------------------------------
-------------------------------------------------
function on.construction()
   var.store("time", {})
   on.timer()
   timer.start(2)
end
function on.timer()
   local elapsedTime, At = elapsedTime_co()
   var.storeAt("time", elapsedTime, At)
end

Reference: