简体   繁体   English

spin.js在长时间运行的任务中不会显示,但是在我逐步调试时会显示

[英]spin.js doesn't show up during long running task, but does when I step through the debugger

I'm using spin.js during a long running processing task. 我在长时间运行的处理任务中正在使用spin.js。

When I just run the task, the UI changes I make immediately before the task runs do not show during the task itself. 当我只运行任务时,在任务本身之前不会立即显示我在任务运行之前所做的UI更改。 However, when I manually step through through the process with the debugger, the UI updates as expected. 但是,当我使用调试器手动完成整个过程时,UI会按预期进行更新。 What gives? 是什么赋予了? I tried running a setTimeout on the long running function to give the UI time to update; 我尝试在长时间运行的函数上运行setTimeout,以给UI时间更新。 no dice. 没有骰子。

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();
}

You have to remember that JS is single threaded. 您必须记住,JS是单线程的。 What happens when you use setTimeout(fn, delay) is that JS queues up the function fn to run approximately delay milliseconds in the future, then continues running the lines following setTimeout() . 当您使用setTimeout(fn, delay)时,会发生什么情况setTimeout(fn, delay)就是JS将函数fn排队以在将来运行大约delay毫秒,然后继续运行setTimeout()之后的行。

This means that your updateGraphDisplay() function will run to completion, including creating the spinner and then milliseconds later, removing it. 这意味着您的updateGraphDisplay()函数将运行完成,包括创建微调器,然后在几毫秒后删除它。 Hence why you don't see it update. 因此,为什么您看不到它更新。

Instead, what you need to do is move everything after your "long running local computations" into the function passed to setTimeout() so it will execute after that code has run in the future. 相反,您需要做的是将“长期运行的本地计算”之后的所有内容移到传递给setTimeout()的函数中,以便它将在以后运行该代码执行。

Abbreviated code structure example: 缩写代码结构示例:

 // initialisation code
 // create spinner and append to DOM
 setTimeout(function () {
     // do calculations
     // update graph
     // clear spinner
 }, 0); // even a delay of 0 will work

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

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