Search code examples
javascriptd3.jswarningsblink

D3.js Warning: Unresponsive Script Message


I'm getting an error message after a while, when using a blink function in 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);})();
            }

enter image description here


Solution

  • As I said in my comment:

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

    To fix this, remove the nesting:

    <!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>

    Of course, if you just want something to blink you can do this 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>