D3.js timer.stop() Function
The timer.stop() function in D3.js is used to stop the current ongoing timer function thus preventing further calls to the function. This function will only work if the timer has not stopped yet.
Syntax:
timer.stop()
Parameters: It takes no parameters.
Returns: It does not return anything.
Note: The Output should be different when run different times with the same code.
Below given are a few examples of the above function.
Example 1: When the timer is stopped and timer.stop() is used after that.
javascript
<!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"
src="https:
</script>
<script>
let func = function (e) {
console.log(
'console.log("Timer stopped") will not be executed' );
if (e > 300) {
console.log("Timer stopped");
timer.stop();
}
timer.stop();
};
var timer = d3.timer(func);
</script>
</body>
</html>
|
Output:
Example 2:
html
<!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 >
< script type = "text/javascript"
</ script >
< script >
let func=function(e) {
// Printing time elapsed
console.log(e)
if (e>=400){
console.log("Timer stopped.")
// Timer stopped
timer.stop();
}
}
// No delay given
var timer = d3.timer(func);
</ script >
</ body >
</ html >
|
Output:
Last Updated :
24 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...