当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


HTML DOM console.timeEnd()用法及代码示例


HTML中的console.timeEnd()方法用于结束由console.time()方法启动的计时器。出于测试目的,这可用于计算某些操作的时间。

用法:

console.timeEnd( label )

参数:此方法接受可选的单个参数标签。用于指定要停止的计时器的标签。


范例1:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        DOM console.timeEnd() Method 
    </title> 
</head> 
  
<body> 
    <h1 style="color:green"> 
            GeeksforGeeks 
        </h1> 
  
    <p>DOM console.timeEnd() Method</p> 
  
    <p> 
        Clicking the START TIMER button executes 
        console.time() to start the timer.<br>  
        Clicking the END TIMER button executes 
        console.timeEnd() to end the timer. 
    </p> 
  
    <button onclick="start_timer()"> 
        Start Timer 
    </button> 
  
    <button onclick="end_timer()"> 
        End Timer 
    </button> 
  
    <script> 
        // Function to start timer 
        function start_timer() { 
            console.log('timer started'); 
            console.time(); 
        } 
  
        // Function to end timer 
        function end_timer() { 
            console.timeEnd(); 
        } 
    </script> 
</body> 
  
</html>      

输出:
timeEnd example 1
控制台视图:
timeEnd example 1 console

范例2:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        DOM console.timeEnd() Method 
    </title> 
</head> 
  
<body> 
    <h1 style="color:green"> 
            GeeksforGeeks 
        </h1> 
  
    <p>DOM console.timeEnd() Method</p> 
  
    <p> 
        Clicking the START TIMER button starts 
        the timer with label 'timer 1'.<br> 
        Clicking the END TIMER button ends the 
        timer with label 'timer 1'. 
    </p> 
  
    <button onclick="start_timer_one()"> 
        Start Timer 1 
    </button> 
  
    <button onclick="end_timer_one()"> 
        End Timer 1 
    </button> 
  
    <p> 
        Clicking the START TIMER button starts  
        the timer with label 'timer 2'.<br>  
        Clicking the END TIMER button ends the 
        timer with label 'timer 2'. 
    </p> 
  
    <button onclick="start_timer_two()"> 
        Start Timer 2 
    </button> 
  
    <button onclick="end_timer_two()"> 
        End Timer 2 
    </button> 
  
    <script> 
        // Start first timer 
        function start_timer_one() { 
            console.log('timer 1 started'); 
            console.time('timer 1'); 
        } 
  
        // End first timer 
        function end_timer_one() { 
            console.timeEnd('timer 1'); 
        } 
  
        // Start second timer 
        function start_timer_two() { 
            console.log('timer 2 started'); 
            console.time('timer 2'); 
        } 
  
        // End second timer 
        function end_timer_two() { 
            console.timeEnd('timer 2'); 
        } 
    </script> 
</body> 
  
</html>

输出:
timeEnd example 2
控制台视图:
timeEnd example 2 console

支持的浏览器:console.timeEnd()方法支持的浏览器如下:

  • 谷歌浏览器
  • Internet Explorer 11.0
  • Firefox 10.0
  • Opera
  • Safari 4.0


相关用法


注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 HTML | DOM console.timeEnd() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。