简体   繁体   English

交互式加载屏幕(根据用户输入或经过的时间更新)

[英]An Interactive Loading Screen (which updates based on user input or time elapsed)

I have a simple Angular app that allows users to do some heavy calculations on their end (must not be on the server).我有一个简单的 Angular 应用程序,它允许用户在他们的端进行一些繁重的计算(不能在服务器上)。 I am currently using a customized ngx-spinner to show a loading screen during the calculation.我目前正在使用定制的 ngx-spinner 在计算过程中显示加载屏幕。 I would like to know (if possible) how to make the loading screen interactive, meaning: can respond to user input or can change the display (eg text or image) as time goes while the calculation is being carried out.我想知道(如果可能的话)如何使加载屏幕具有交互性,这意味着:可以响应用户输入或可以在执行计算时随着时间的推移更改显示(例如文本或图像)。

You can imagine it as a mini-game that a user can play while it is loading, a count-down timer, or even just a clock that shows the current time.你可以把它想象成一个用户可以在加载时玩的小游戏、一个倒数计时器,甚至只是一个显示当前时间的时钟。 Any of them would be great.他们中的任何一个都会很棒。

The flow would look like this:流程如下所示:

(1) A user issues a calculation command (1) 用户发出计算命令

(2) Show the loading screen and then start the calculation (2) 显示加载屏幕,然后开始计算

  • user should be able to click on the loading screen for some effects, and/or see changes as time goes while waiting for the result用户应该能够在加载屏幕上单击某些效果,和/或在等待结果时看到随着时间的推移而发生的变化

(3) calculation is done, display the result and hide the loading screen (3) 计算完成,显示结果并隐藏加载画面

So far I have tried to set up a timer (either using rxjs or setInterval) to periodically update it or bind the HTML element with an event handler but they only work when there's nothing running synchronously (if I simply show the loading screen without doing any calculation).到目前为止,我已经尝试设置一个计时器(使用 rxjs 或 setInterval)定期更新它或将 HTML 元素与事件处理程序绑定,但它们仅在没有同步运行的情况下工作(如果我只是显示加载屏幕而不做任何事情计算)。 I know it probably has something to do with the fact that the UI thread(?) is occupied by the calculation but I don't know any way to circumvent that (or to use any other thread).我知道这可能与 UI 线程(?)被计算占用的事实有关,但我不知道有什么方法可以绕过它(或使用任何其他线程)。 Is this a limitation of single-page applications or is it just not possible in general?这是单页应用程序的限制,还是一般来说不可能? (If yes, is there any way to fake it and make it look like what I described? And not just using a loading gif.) (如果是的话,有什么方法可以伪造它并让它看起来像我描述的那样?而不仅仅是使用加载 gif。)

First Edit: I have just found a similar and (easier?) question: Update webpage to show progress while javascript is running in in a loop but could be a bit limited.第一次编辑:我刚刚发现了一个类似的(更简单的?)问题: 更新网页以显示进度,而 javascript 正在循环运行,但可能有点受限。 I still like to know if there is a general solution without needing to split up every long process to indicate progress but simply update the spinner according to a timer (or user interaction) regardless.我仍然想知道是否有一个通用的解决方案,而不需要拆分每个长流程来指示进度,而只需根据计时器(或用户交互)更新微调器。

Disclaimer: I'm fairly new to Angular, as well as real-world apps.免责声明:我对 Angular 以及现实世界的应用程序相当陌生。 Apologies for any misuse of languages or any presence of dumb ideas.对任何滥用语言或任何愚蠢想法的存在表示歉意。

If part of the problem is that the web page is 'frozen' or 'stalled' while waiting for the calculation to complete, it might be possible for you to have the calculation itself performed in a web worker: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers如果部分问题是 web 页面在等待计算完成时被“冻结”或“停止”,您可能会在 web 工作人员中执行计算本身: Z5E056C500A1C48BADE51 .org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

You can do something like:您可以执行以下操作:

  1. Create a web worker file and put some logic in there which will perform the calculations.创建一个 web 工作文件并在其中放置一些逻辑来执行计算。
  2. Add events for "onCalculationReceived" and "onCalculationComplete".为“onCalculationReceived”和“onCalculationComplete”添加事件。 The web worker will listen for the first event, perform calcuations, and dispatch the second. web worker 将监听第一个事件,执行计算,并调度第二个事件。
  3. Where the calculation is invoked, dispatch the first event and wait for the second event.在调用计算的地方,调度第一个事件并等待第二个事件。

With that, you should have no problems with letting the user interact with the UI while the expensive calculation is being performed.这样,在执行昂贵的计算时让用户与 UI 交互应该没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM