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


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

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>

輸出:




相關用法


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