有人可以调试我的 Javascript 小程序中的进度条问题吗?

发布于 2024-12-09 06:41:46 字数 401 浏览 0 评论 0原文

这是小程序

您可以保留所有设置不变,然后点击“计算”。几秒钟后,您应该会看到一些图表出现,然后“计算”按钮下方的进度条将填充到 100%。

问题是我希望进度条在代码运行时增加,而不是在绘图完成后增加。我知道代码很长,但是您可以搜索以下进度条代码:

setTimeout( update(count++, L.length, f.length, phi.length) );

它访问直接在主calculate()函数之前定义的函数update(s,x,y,z)。

我只是很困惑为什么进度条在所有处理完成之前不会更新。

提前致谢!

Here is the applet

You can leave all the settings as they are, then hit "Calculate". After a few seconds, you should see some plots show up, then the progress bar below the "Calculate" button will fill up to 100%.

The problem is that I'd like the progress bar to increment while the code is running, not after it has completed as the plots are made. I know the code is long, but you can search for the following progress bar code:

setTimeout( update(count++, L.length, f.length, phi.length) );

It accesses the function update(s,x,y,z) which is defined directly before the main calculate() function.

I'm just confused as to why the progress bar doesn't update until all the processing is complete.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

止于盛夏 2024-12-16 06:41:46

您观察到的问题是由 javascript 的单线程性质引起的。执行现有代码时不会执行超时。引入 Web Worker 就是为了处理这种类型的限制。如果您无法使用 Web Worker(例如由于 IE 限制),则使用超时是模拟多线程的唯一方法。

要检查这一点,您可以将行替换为 update(count++, L.length, f.length, phi.length)() 并将 update 更改为将值记录到控制台。

至于当前的更新实现,我想要么是 jquery 内部有一些迷你计时器导致了问题,要么是浏览器渲染功能由于某种原因不可用:)。

原因是你的计算和绘图所花费的时间远远少于超时时间。因此,当执行超时函数时,所有内容都已绘制完毕。
您可以通过用 update(count++, L.length, f.length, phi.length)() 替换您的行来验证这一点 - 它将导致即时更新高达 100%。

为了使进度条工作更精确,您应该检查您的计算代码并尝试以块友好的方式重写它。我的意思是有可能逐块执行计算并在每个块末尾更新进度条。完成后,您可以使用计时器来评估一切。

示例:

(function(){
    var arr = [1,2,3,4];
    (function popLog(){
        console.log(arr.pop());
        if (arr.length > 0){
            setTimeout(popLog, 100);
        }
    })();
})()

另一种方法是使用 Web Workers,不时查询状态并适当更新进度栏。但是,IE 不支持它们。

The problem you observe is caused by javascript's single-threaded nature. Timeouts do not get executed while existing code is being executed. Web workers were introduced to deal with just that type of restriction. If you are unable to use web workers(e.g. due to IE limitations), using timeouts is the only way to emulate multythreading.

To check that, you may replace your line with update(count++, L.length, f.length, phi.length)() and change update to log values into console.

As for current update implementation, I suppose either there are some any mini timers inside jquery, causing the issue, or browser rendering capabilities are just not available for some reason:).

The reason is that your calculations and drawings take far less time than timeout. Therefore by the time timeout functions are executed, everything has already been drawn.
You could verify that by replacing your line with update(count++, L.length, f.length, phi.length)() - it will lead to instant update up to 100%.

To get progress bar work more precise you should review your calculation code and try rewriting it in a chunk-friendly manner. I mean having a possibility to execute calculations chunk by chunk and updating progress bar at the end of each chunk. Having that done, you could use timer to evaluate everything.

Sample:

(function(){
    var arr = [1,2,3,4];
    (function popLog(){
        console.log(arr.pop());
        if (arr.length > 0){
            setTimeout(popLog, 100);
        }
    })();
})()

Another approach is using web workers, querying the status from time to time and updating progress bar appropriately. However, IE does not support them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文