spin.js没有'它不会在长时间运行的任务中显示,但当我逐步通过调试器时会显示
spin.js doesn't show up during long running task, but does when I step through the debugger
我在一个长时间运行的处理任务中使用spin.js。
当我刚运行任务时,在任务运行之前所做的UI更改不会在任务本身中显示。但是,当我使用调试器手动完成整个过程时,UI会按预期进行更新。什么东西?我试着在长时间运行的函数上运行setTimeout,给UI更新的时间;没有骰子。
function updateGraphDisplay() {
var opts = {
lines: 13, // The number of lines to draw
length: 20, // The length of each line
width: 10, // The line thickness
radius: 30, // The radius of the inner circle
corners: 1, // Corner roundness (0..1)
rotate: 0, // The rotation offset
direction: 1, // 1: clockwise, -1: counterclockwise
color: '#000', // #rgb or #rrggbb or array of colors
speed: 1, // Rounds per second
trail: 60, // Afterglow percentage
shadow: false, // Whether to render a shadow
hwaccel: false, // Whether to use hardware acceleration
className: 'spinner', // The CSS class to assign to the spinner
zIndex: 2e9, // The z-index (defaults to 2000000000)
top: '50%', // Top position relative to parent
left: '50%' // Left position relative to parent
};
var spinner = new Spinner(opts).spin();
$('#chart').append(spinner.el);
// long running local computations (~4s)
Highcharts.charts[0].series[0].setData(sentimentDataPoints);
Highcharts.charts[0].series[1].setData(volumeDataPoints);
spinner.stop();
}
您必须记住JS是单线程的。当您使用setTimeout(fn, delay)
时,JS会将函数fn
排队,以便在未来运行大约delay
毫秒,然后继续运行setTimeout()
之后的行。
这意味着你的updateGraphDisplay()
函数将运行到完成,包括创建微调器,然后在几毫秒后删除它。因此,你没有看到它更新。
相反,您需要做的是将"长时间运行的本地计算"之后的所有内容移动到传递给setTimeout()
的函数中,这样它将在未来代码运行后执行。
缩写代码结构示例:
// initialisation code
// create spinner and append to DOM
setTimeout(function () {
// do calculations
// update graph
// clear spinner
}, 0); // even a delay of 0 will work
相关文章:
- 在等待ajax请求时显示微调器并禁用页面
- VS2010 javascript调试器希望在VS2010的新实例中启动
- 变量在运行时未定义,但在使用调试器时定义
- 调试器;[错误]194:11:标识符是一个保留字
- 在页面检查器中启用javascript调试器
- console.assert失败时中断chrome调试器
- 如何在长时间执行JavaScript期间显示微调器
- 仅打开调试器时出现Google chrome扩展错误
- Chrome调试器中显示的“类名”基于什么?能控制吗?
- 为什么某些Javascript属性没有在调试器中以交互方式显示
- 嵌套循环中出现意外中断;t显示在调试器中
- spin.js没有'它不会在长时间运行的任务中显示,但当我逐步通过调试器时会显示
- Firefox没有处理keyCode's.它在调试器中将变量显示为矩阵
- 在Chrome调试器中显示为同时具有值和未定义的变量
- 调试器显示错误的信息与源映射
- 用调试器命令在仿真模式下启动Chrome,但页面显示为PC版本
- Rails在调试器中显示部分Ajax错误但正确的响应
- JavaScript调试器显示对象属性为双冒号,Handlebars无法渲染
- Emberjs -调试器在{{#each}}未加载模型时不显示错误
- 为什么当我在调试器中看到更改时,元素的属性没有显示为已更改