HomeLanguagesJavascriptD3.js interrupt() Function

D3.js interrupt() Function

The d3.interrupt() function in D3.js is used to interrupts the active transition of the specified name on the specified node and cancels any pending transitions with the specified name. This function is similar to the selection.interrupt() function.

Syntax:

d3.interrupt(node[, name])

Parameters: This function accepts the following parameter as mentioned above and described below:

  • name: This parameter is the transition instance.
  • node: This parameter is the node for the passing argument.

Return Value: This function returns an interrupted active transition.

Below programs illustrate the d3.interrupt() function in D3.js.

Example 1:




<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8">
    <script src="https://d3js.org/d3.v5.min.js"> 
    </script>
</head> 
        
<body> 
    <center>
        <h1 style="color: green;"> 
            Geeksforneveropen 
        </h1> 
          
        <h3>D3.js | d3.interrupt() Function</h3>
           
        <svg width="400" height="250"></svg>
           
        <script>
            var svg = d3.select("svg")
              
            var circle = svg.selectAll("circle")
                .data([1, 2, 3, 4])
                .enter()
                .append("circle")
                .style("fill", "red")
                .attr("cx", 50)
                .attr("cy", function(d) {
                    return d * 50
                })
                .attr("r", 25)
                .on("click", function() {
                    d3.interrupt(d3.select(this))
                })
              
            circle.transition()
                .delay(function(d) {
                    return d * 500;
                })
                .duration(function(d) {
                    return d * 1000;
                })
                .attr("cx", 360)
                .on("interrupt", function() {
                    var elem = this;
                    var targetValue = d3.active(this)
                    .attrTween("cx")                  
                    .call(this)(1);
                    d3.select(this).attr("cx", targetValue)
                })
        </script> 
    </center>
</body> 
</html>


Output:

Example 2:




<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8">
    <script src="https://d3js.org/d3.v5.min.js"> 
    </script>
       
    <style>
        svg {
          background-color: green;
          display: block;
        };
    </style>
</head> 
        
<body> 
    <center>
        <h1 style="color: green;"> 
            Geeksforneveropen 
        </h1> 
          
        <h3>D3.js | d3.interrupt() Function</h3>
           
        <button>Stop</button>
        <svg width="500" height="150"></svg>
           
        <script>
            const svg = d3.select("svg");
            const local = d3.local();
            const button = d3.select("button");
            const circle = svg.append("circle")
                .attr("r", 25)
                .attr("cx", 30)
                .attr("cy", 75)
                .style("fill", "yellow")
                .style("stroke", "black");
               
            circle.transition()
                .delay(5000)
                .duration(10000)
                .ease(d3.easeLinear)
                .attr("cx", 580)
                .on("interrupt", function() {
                    local.set(this, +d3.select(this)
                    .attr("cx"))
                });
               
            button.on("click", function() {
                if (d3.active(circle.node())) {
                    d3.interrupt(circle);
                    this.textContent = "Resume";
                } 
                else {
                    circle.transition()
                    .ease(d3.easeLinear)
                    .duration(function() {
                        return 10000 * (560 -
                        local.get(this)) / 560;
                    })
                    .attr("cx", 580)
                    this.textContent = "Stop";
                }
            })
        </script> 
    </center>
</body>  
</html>


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS