The D3.timer() function is used to run a timer function after a particular interval of time. The timer will run after a specified delay. The delay given is in milliseconds.
Syntax:
d3.timer(callback, delay);
Parameters: It takes the following two parameters.
- callback: It is a function.
- delay: It is the delay after which the function is executed.
Returns: It returns the Time of datatype Number.
Below given are a few examples of the above function.
Example 1:
<!DOCTYPE html> < html lang = "en" > < head > Â Â < meta charset = "UTF-8" > Â Â < meta name = "viewport" Â Â Â Â Â Â Â Â content=" width = device -width, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â initial-scale = 1 .0"> Â Â < title >Document</ title > </ head > < style > </ style > < body > Â Â <!-- Fetching from CDN of D3.js --> Â Â < script type = "text/javascript" Â Â </ script > Â Â < script > Â Â let func=function(elapsed) { Â Â Â Â Â Â console.log(elapsed); Â Â Â Â Â Â if (elapsed > 500){ Â Â Â Â Â Â Â Â console.log("Timer stopped") Â Â Â Â Â Â Â Â timer.stop(); Â Â Â Â Â Â } Â Â Â Â } Â Â Â var timer = d3.timer(func); Â Â </ script > </ body > </ html > |
Output:
Example 2:
<!DOCTYPE html> < html lang = "en" > < head >   < meta charset = "UTF-8" >   < meta name = "viewport"         content=" width = device -width,                  initial-scale = 1 .0">   < title >Document</ title > </ head > < style >   .originalColor{     height: 100px;     width: 100px;   }   .darkerColor{     height: 100px;     width: 100px;   } </ style > < body >   <!-- Fetching from CDN of D3.js -->   < script type = "text/javascript"    </ script >   < script >   let func=function(e) {       console.log(e);       if (e>300){         console.log("Timer stopped")         timer.stop();       }     }     // Delay of 2000ms    var timer = d3.timer(func, 2000);   </ script > </ body > </ html > |
Output: