Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptD3.js interval() Function

D3.js interval() Function

The d3.interval() function is used to call the function after every given time interval or delay. If the delay is not given then the delay is equal to the timer.

Syntax:

d3.interval(callback, delay);

Parameters: It takes the two parameters as mentioned above and described below:

  • callback: It is the function to be executed after a particular delay.
  • delay: It is the delay after which the function is executed.

Returns: It returns an object.

Note: The output may be different every time the code is executed.

Example 1: When no delay is given.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
  
    <style>
        .originalColor {
            height: 100px;
            width: 100px;
        }
  
        .darkerColor {
            height: 100px;
            width: 100px;
        }
    </style>
</head>
  
<body>
    <!-- Fetching from CDN of D3.js -->
    <script type="text/javascript" 
        src="https://d3js.org/d3.v4.min.js">
    </script>
      
    <script>
        let count = 0;
        let func = function (e) {
            count++;
            console.log(e);
            console.log("Time interval will "
                + "stop when count is greater 5" 
                + "current count: ", count);
            if (count > 5)
                timer.stop()
        }
        var timer = d3.interval(func);
    </script>
</body>
  
</html>


Output:

Example 2: When a delay is given.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
  
    <style>
        .originalColor {
            height: 100px;
            width: 100px;
        }
  
        .darkerColor {
            height: 100px;
            width: 100px;
        }
    </style>
</head>
  
<body>
    <!-- Fetching from CDN of D3.js -->
    <script type="text/javascript" 
        src="https://d3js.org/d3.v4.min.js">
    </script>
      
    <script>
        let count = 0;
        console.log("The delay is 500ms.");
        let func = function (e) {
            count++;
            console.log(e);
            console.log("Time interval will "
                + "stop when count is greater 3" 
                + " current count: ", count);
            if (count > 3)
                timer.stop()
        }
        var timer = d3.interval(func, 500);
    </script>
</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

Most Popular

Recent Comments