d3.interval()函數用於在每個給定的時間間隔或延遲後調用該函數。如果沒有給出延遲,則該延遲等於計時器。
用法:
d3.interval(callback, delay);
參數:它采用上述和以下所述的兩個參數:
- callback:它是在特定延遲後執行的函數。
- delay:這是執行函數之後的延遲。
返回值:它返回一個對象。
注意:每次執行代碼時,輸出可能會不同。
範例1:沒有延遲時。
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>
輸出:
範例2:延遲時。
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>
輸出:
相關用法
- JQuery jQuery.fx.interval用法及代碼示例
- d3.js d3.map.get()用法及代碼示例
- PHP abs()用法及代碼示例
- PHP Ds\Map put()用法及代碼示例
- d3.js d3.map.has()用法及代碼示例
- PHP pow( )用法及代碼示例
- CSS rgb()用法及代碼示例
- d3.js d3.max()用法及代碼示例
- PHP Ds\Map xor()用法及代碼示例
- p5.js value()用法及代碼示例
- PHP Ds\Set add()用法及代碼示例
- PHP sin( )用法及代碼示例
- PHP cos( )用法及代碼示例
- p5.js arc()用法及代碼示例
注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js interval() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。