我有一个运行模拟,其中我想渲染的结果“活”一个小脚本:如何暂停JavaScript来允许渲染
for (i < simulation steps) {
do_simulation();
render_with_flot();
}
我注意到,该地块只盯着最后的渲染步。
- 有没有办法'暂停'JavaScript以某种方式允许渲染在每次迭代后运行?
- 或者有没有办法让flot同步运行?
- 或者我是否需要为for循环的每次迭代设置自己的超时?这似乎是一种麻烦。
我有一个运行模拟,其中我想渲染的结果“活”一个小脚本:如何暂停JavaScript来允许渲染
for (i < simulation steps) {
do_simulation();
render_with_flot();
}
我注意到,该地块只盯着最后的渲染步。
作为@ PhonicUK解决方案的替代方案,您可以在工作结束时()安排一次setTimeout()来安排下一次调用工作(),从而使渲染有机会发生,不会让您自己遇到任何特定的刷新率。
取决于它需要多快的速度运行,但最好的方法是使用SetInterval
伪/手写的JavaScript,也就是说,大概-犯规运行:
var PerformedSteps;
var Interval;
PerformedSteps = 0;
Interval = setInterval(work, 1000/60); //60 times/second
function work()
{
PerformedSteps++;
if (PerformedSteps == simulation_steps)
{
clearInterval(Interval);
return;
}
do_simulation();
render_with_flot();
}
当任务持续超过1000/60 ms时,这是否不会产生不同循环迭代重叠的风险? – noio 2013-05-15 09:54:46
你的意思是'setTimeout'? – noio 2013-05-14 13:59:14