Wednesday, October 22, 2025
HomeLanguagesJavascriptD3.js timer.restart() Function

D3.js timer.restart() Function

The timer.restart() function in D3.js is used to restart a timer with the given function and delay. The timer.restart() function is used when one wants to reset the timer and start again.

Syntax:

timer.restart(callback, delay);

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

  • callback: It is the function to be stopped or start after a particular delay.
  • delay: It is the time after which the function will be executed or stop

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">
</head>
  
<body>
    <!-- Fetching from CDN of D3.js -->
    <script type="text/javascript" 
        src="https://d3js.org/d3.v4.min.js">
    </script>
      
    <script>
        count = 0;
        let func = function (e) {
            console.log(e)
            if (e > 40) {
                console.log("Timer stopped after 40ms")
                if (e > 40) {
                    count++;
  
                    // Restarting the timer again
                    console.log("Timer restarts")
                    timer.restart(func)
                }
                if (count > 2) {
                    timer.stop();
                    console.log(
                        "count > 2 so timer is stopped")
                }
            }
        }
        var timer = d3.timer(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">
</head>
  
<body>
    <!-- Fetching from CDN of D3.js -->
    <script type="text/javascript" 
        src="https://d3js.org/d3.v4.min.js">
    </script>
      
    <script>
        count = 0;
        let func = function (e) {
            console.log(e)
            if (e > 10) {
                console.log("Timer stopped after 10ms")
                if (e > 10) {
                    count++;
  
                    // Restarting the timer again
                    console.log("Timer restarts")
                    timer.restart(func)
                }
                if (count > 4) {
                    timer.stop();
                    console.log(
                    "count > 4 so timer is stopped")
                }
            }
        }
        // A delay of 2000ms
        var timer = d3.timer(func, 2000);
    </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

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS