D3.js 警告:脚本消息无响应

D3.js Warning: Unresponsive Script Message

在 D3 中使用闪烁功能时,一段时间后我收到一条错误消息

.transition()
                                .duration(250)
                                .delay( 0)
                                .each(blink);

function blink() {

                        var line = d3.select(this);
                        (function repeat() {
                        line = line.transition()
                        .style("opacity", function(d) { return(1);})
                        .transition()
                        .style("opacity", function(d) { return(0);})
                        .each("end", repeat);})();
            }

正如我在评论中所说:

You've created a nested transition, every 17ms, you are creating another transition which in turn fires every 17ms...

要解决此问题,请移除嵌套:

<!DOCTYPE html>
<html>

  <head>
    <script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
  </head>

  <body>
    <script>
      
      var svg = d3.select('body')
        .append('svg')
        .attr('width', 300)
        .attr('height', 300);
        
      svg
        .append('line')
        .attr('y1', 150)
        .attr('y2', 150)
        .attr('x1', 0)
        .attr('x2', 300)
        .style('fill', 'none')
        .style('stroke', 'steelblue');
        
      svg.selectAll("line")
        .each(blink);
        
      function blink(){
        var self = d3.select(this);
        self
          .transition()
          .duration(250)
          .style("opacity", function(d) { 
            return self.style("opacity") === "1" ? "0" : "1";
          })
          .each("end", blink);
      }
      
    </script>
  </body>

</html>

当然,如果你只是想让东西闪烁,你可以这样做with straight css:

<!DOCTYPE html>
<html>

  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
    <style>
.blink {
  animation: blink-animation 1s steps(5, start) infinite;
  -webkit-animation: blink-animation 1s steps(5, start) infinite;
}
@keyframes blink-animation {
  to {
    visibility: hidden;
  }
}
@-webkit-keyframes blink-animation {
  to {
    visibility: hidden;
  }
}
    </style>
    
  </head>

  <body>
    <script>
      
      var svg = d3.select('body')
        .append('svg')
        .attr('width', 300)
        .attr('height', 300);
        
      svg
        .append('line')
        .attr('class', 'blink')
        .attr('y1', 150)
        .attr('y2', 150)
        .attr('x1', 0)
        .attr('x2', 300)
        .style('fill', 'none')
        .style('stroke', 'steelblue');       

    </script>
  </body>

</html>