D3.js中的timer.restart()函數用於以給定的函數和延遲重新啟動定時器。如果要重置計時器並重新啟動,則使用timer.restart()函數。
用法:
timer.restart(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">
</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>
輸出:
範例2:延遲時。
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>
輸出:
相關用法
- PHP Ds\Map xor()用法及代碼示例
- p5.js value()用法及代碼示例
- PHP Ds\Map put()用法及代碼示例
- CSS url()用法及代碼示例
- p5.js mag()用法及代碼示例
- PHP ord()用法及代碼示例
- PHP Ds\Map get()用法及代碼示例
- p5.js nfc()用法及代碼示例
- p5.js nfp()用法及代碼示例
- p5.js nfs()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- p5.js box()用法及代碼示例
- PHP each()用法及代碼示例
- PHP Ds\Set add()用法及代碼示例
- p5.js nf()用法及代碼示例
注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js timer.restart() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。