當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


d3.js timer()用法及代碼示例

D3.timer()函數用於在特定時間間隔後運行計時器函數。計時器將在指定的延遲後運行。給出的延遲以毫秒為單位。

用法:

d3.timer(callback, delay);

參數:它采用以下兩個參數。

  • callback:這是一個函數。
  • delay:這是執行函數之後的延遲。

返回值:它返回數據類型編號的時間。

下麵給出了上述函數的一些示例。



範例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"
          src = "https://d3js.org/d3.v4.min.js"> 
  </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>

輸出:

範例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" 
          src = "https://d3js.org/d3.v4.min.js"> 
  </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>

輸出:




相關用法


注:本文由純淨天空篩選整理自tarun007大神的英文原創作品 D3.js timer() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。