如何暂停JavaScript以允许渲染 [英] How to suspend JavaScript to allow render

查看:89
本文介绍了如何暂停JavaScript以允许渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行模拟的小脚本,我想对其进行实时"渲染:

I have a little script that runs a simulation, of which I want to render the results 'live':

for ( i < simulation steps ) {
    do_simulation();
    render_with_flot();
}

我注意到情节仅在最后一步之后才渲染.

I noticed that the plot only gets rendered after the last step.

  • 是否有办法以某种方式暂停" javascript以允许渲染在每次迭代后运行?
  • 还是有一种方法可以使flot同步运行?
  • 还是我需要为for循环的每次迭代设置自己的超时时间?这似乎有点麻烦.

推荐答案

作为@PhonicUK解决方案的替代方案,您可以在work()的末尾执行setTimeout()以安排对work()的下一次调用,呈现发生的机会&不要将自己束缚在任何特定的刷新率上.

As an alternative to @PhonicUK's solution, you could do a setTimeout() at the end of work() to schedule the next call to work(), giving the rendering a chance to happen & not tying yourself to any particular refresh rate.

这篇关于如何暂停JavaScript以允许渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆