0

这是一个与js 处理完成时基本 Javascript 加载消息相关的问题

我的主要问题是在我的两个函数 drawlegend() 和 display() 被调用之前光标没有改变,但是在一切都完成后改变。使用下面的代码恢复光标临时注释掉,我得到沙漏,但一切都完成了。

如何在调用慢速函数之前让光标变为沙漏?

examplefunc()
{
   mini.append("text")
    .text(series[i].name)
    .attr("x",30)
    .attr("y",(15+(15*i)))
    .attr("stroke",(d3.rgb(192,192,192)))
    .attr("fill",(d3.rgb(192,192,192)))
    .attr("stroke-width",0)
    .style("font-size","12px")
    .attr("text-anchor","start")
    .attr("id","legend")
    .on('mouseup', legendclick);
}

//===== legend clicked
function legendclick()
{
   //--- get mouse pos
   var origin = d3.mouse(this);

   //---  get channel
   var ch=Math.floor((origin[1]-4)/15);

   //--- toggle active state
   if (series[ch].active==true)
      series[ch].active=false;
   else
      series[ch].active=true;

   setTimeout(setcursor("wait"),5);         
   drawlegend();
   display();
   //setTimeout(setcursor("default"),5); // temp removed to see any result at all
 }

//===== set cursor
function setcursor(cursor)
{
  d3.select("body").style("cursor", cursor);
}
4

1 回答 1

4

众所周知,在 javascript 中执行操作会挂起您的应用程序。这意味着只有最终输出显示在您的屏幕上。因此,当您将光标更改为“等待”并在执行后更改为“光标”时,javascript 并没有更改它,因为 ui 线程正忙于计算函数“drawlegend”和“display”中的内容。但是,我认为当您执行“drawlegend”和“display”异步时

setTimeout(function () {
        drawLegend();
        display();
        setcursor("default");
}, 0);

那么事情应该如你所愿。让我知道这是否适合您。

额外信息:在此幻灯片(尤其是幻灯片 5)上解释了您的问题是什么。

于 2013-08-16T06:14:00.640 回答