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


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

D3.js中的timer.stop()函數用於停止當前正在進行的計時器函數,從而防止對該函數的進一步調用。僅當計時器尚未停止時,此函數才起作用。

用法:

timer.stop()

參數:它不帶任何參數。

返回:它不返回任何東西。

注意:當使用同一代碼在不同時間運行時,輸出應該是不同的。



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

範例1:當計時器停止並在之後使用timer.stop()時。

<!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 (e) { 
                console.log( 
'console.log("Timer stopped") will not be executed'); 
                if (e > 300) { 
                    console.log("Timer stopped"); 
// This will have no effect as timer is stopped already 
                    timer.stop(); 
                } 
                // Timer stopped 
                timer.stop(); 
            }; 
            // Delay of 2000ms 
            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) { 
    // 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>

輸出:




相關用法


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