Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

HTML | DOM console.time() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The console.time() method in HTML is used to starting a timer in the console view. The console.time() method can be used for calculating the time of programs for various testing purposes. The label is sent as a parameter to the console.time() method

Syntax:

console.time( label )

Parameters: This method accepts single parameter label which is optional and used to specify the label of the timer. 

Below programs illustrate the console.time() method in HTML: 

Example 1: 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM console.time() Method in HTML</title>
    <style>
        h1 {
            color: green;
        }
         
        h2 {
            font-family: Impact;
        }
         
        body {
            text-align: center;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h2>DOM console.time() Method</h2>
    <p>
      To view the message in the console
      press the F12 key on your keyboard.
    </p>
    <p>
      To view the time taken by a for loop
      to run 100 times, double click the
      button below:
    </p>
    <br>
    <button ondblclick="table_time()">
      RUN TIMER
    </button>
    <script>
        function table_time() {
            console.time();
            for (i = 0; i < 100; i++) {
                // Random code
            }
            console.timeEnd();
        }
    </script>
</body>
 
</html>  

Output:

  

Console View:

  

Example 2: Calculate the time taken by a while loop using console.time() method 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM console.time() Method in HTML</title>
    <style>
        h1 {
            color: green;
        }
         
        h2 {
            font-family: Impact;
        }
         
        body {
            text-align: center;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h2>DOM console.time() Method</h2>
    <p>
      To view the message in the console
      press the F12 key on your keyboard.
    </p>
    <p>
      To view the time taken by a for loop
      to run 100 times, double click the
      button below:
    </p>
    <br>
    <button ondblclick="table_time()">
      RUN TIMER
    </button>
    <script>
        function table_time() {
            i = 0;
            console.time
                ("While loop for 100 iterations");
            while (i < 100) {
                i++;
            }
            console.timeEnd
                ("While loop for 100 iterations");
        }
    </script>
</body>
 
</html>

Output:

  

Console View:

  

Supported Browsers: The browser supported by console.time() Method are listed below:

  • Google Chrome 1.0
  • Edge 12.0
  • Internet Explorer 11.0
  • Firefox 10.0
  • Opera 11.0
  • Safari 4.0

My Personal Notes arrow_drop_up
Last Updated : 04 Jul, 2022
Like Article
Save Article
Similar Reads
Related Tutorials