Replies: 8 comments
-
Yes that is how javascript/the web works.
Share what you tried and maybe someone can help |
Beta Was this translation helpful? Give feedback.
-
Hello Daurnimator. I read that the proper way to this would be with web workers but I am not competent enough to work out how to convert the js code to Lua/Fengari, if it's possible. The slow subroutine would go into a worker from which the main script could receive and display updates. Does anyone have any fengari code for web workers? I might post this in the Lua reddit group a bit later as I got some very helpful replies there in the past. Thank you. |
Beta Was this translation helpful? Give feedback.
-
Hello again, I cracked it, I think, anyway, by splitting the long calculation up into 10 chunks and calling a setTimeout after each to update the progress bar. This is probably the ugliest way of doing it but it works and is only a bit slower than the first version. Still it probably will keep the user informed as to what is going on. I think it could be called a recursion ;-? ;-?
|
Beta Was this translation helpful? Give feedback.
-
You may want to try a coroutine: local function dom_yield()
local co = coroutine.running()
js.global:setTimeout(function() assert(coroutine.resume(co)) end)
return coroutine.yield()
end
coroutine.wrap(function()
for i=1,10^7 do
if i%10^6==0 then
local prog=math.floor(i/10^7*100)
print(prog) --prints out in real time
dom_yield()
end
end
end)() |
Beta Was this translation helpful? Give feedback.
-
Thank you very much for that. The co-routine way is quite a bit quicker. Very interesting and slots in to my main script very easily. Nice one, top one, sorted.
|
Beta Was this translation helpful? Give feedback.
-
My main project has two slow steps so I am now trying to get two progress bars running, one for each, but I am struggling. They both run at the same time even though I use different progress bar id's, different dom_yield functions and call the coroutines co1, co2, etc. Any clues much appreciated, thank you. |
Beta Was this translation helpful? Give feedback.
-
I cracked it to some extent with the following code, but there is a 10 second delay between the completion of the first progress bar and the starting of the next.
If instead I want to have one progress bar that monitors both steps, then there is no delay if I write the code as follows:
That is probably the best solution, but just curious why the 10 second delay arises. I was getting a similar delay at the start with my second example until I put the "progbar.value=tostring(prog)" into dom_yield. No worries anyway, but very much appreciated if you can have a look at this. |
Beta Was this translation helpful? Give feedback.
-
It's working quite well in my main app now, so happy to close. |
Beta Was this translation helpful? Give feedback.
-
I am trying to make a progress bar as follows. I am only able to write very simple code and my test example is here:
The problem is that the progress bar only updates when the calculation has finished while the printed output to the console appears in real time. I have tried using promises and timeouts, etc, but I can't sort it out, sorry. Any help much appreciated.
Beta Was this translation helpful? Give feedback.
All reactions