The d3.timeout() function in D3.js is used to automatically stop the function or the timer after a particular interval of time. It works same as setTimeOut() function in JavaScript.
Syntax:
d3.timeout(callback, delay);
Parameters: This function accepts two parameters as mentioned above and described below:
- callback: It is the function to be stopped after a particular delay.
- delay: It is the time after which the function will be stopped.
Return Value: This function returns an object.
Below given are a few examples of the above function.
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" Â Â Â Â Â </ script > Â Â Â Â Â Â Â Â Â Â < script > Â Â Â Â Â Â Â Â let delay = 0 Â Â Â Â Â Â Â Â let func = function (e) { Â Â Â Â Â Â Â Â Â Â Â Â console.log(e); Â Â Â Â Â Â Â Â Â Â Â Â console.log("It will run one time" Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â + " with delay equal ", delay); Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â var timer = d3.timeout(func, delay); Â Â Â Â Â Â Â Â Â Â func = function (e) { Â Â Â Â Â Â Â Â Â Â Â Â console.log(e); Â Â Â Â Â Â Â Â Â Â Â Â console.log( Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "It will run one time with no delay"); Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â var timer = d3.timeout(func); Â Â Â Â Â Â Â Â console.log("Return Type is: ", typeof timer); Â Â Â Â </ script > </ body > Â Â </ html > |
Output:
Example 2: When the 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" Â Â Â Â Â </ script > Â Â Â Â Â Â Â Â Â Â < script > Â Â Â Â Â Â Â Â let delay = 1000 Â Â Â Â Â Â Â Â let func = function (e) { Â Â Â Â Â Â Â Â Â Â Â Â console.log(e); Â Â Â Â Â Â Â Â Â Â Â Â console.log("It will run one time" Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â + " with delay equal ", delay); Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â var timer = d3.timeout(func, delay); Â Â Â Â Â Â Â Â Â Â func = function (e) { Â Â Â Â Â Â Â Â Â Â Â Â console.log(e); Â Â Â Â Â Â Â Â Â Â Â Â console.log("This will be printed first"); Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â var timer = d3.timeout(func); Â Â Â Â </ script > </ body > Â Â </ html > |
Output: