Open In App

HTML DOM console time() Method

Last Updated : 03 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The console.time() method in HTML is used to start 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 a single parameter label which is optional and used to specify the label of the timer. 

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

html




<!DOCTYPE html>
<html>
   
<head>
    <title>DOM console.time() Method in HTML</title>
    <style>
        h1 {
            color: green;
        }
    </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:

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;
        }
    </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:

Supported Browsers: The browser is supported by the console.time() Methods are listed below:

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


Previous Article
Next Article

Similar Reads

Difference between console.dir and console.log
Console Object: The Console object provides access to the browser's debugging console, which can be seen using F12 or ctrl+shift+j. Console object contains many methods, log() and dir() are most used among. The console.log() method prints out a toString representation of the object in the console to the user. Syntax: console.log(object) or console.
2 min read
HTML DOM console warn() Method
The console.warn() method is used to write a warning message in the console. So open the console to display the output (warning message). Syntax: console.warn( message )Parameters: This method accepts single parameter message which is mandatory. This parameter is used to hold the warning message. Example 1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html
1 min read
HTML DOM console clear() Method
The console.clear() method in HTML is used to clear the console and writes some message "Console was cleared" on the console whenever it is executed. This method does not require any parameter. Syntax: console.clear()Example: The below program illustrates the console.clear() method in HTML: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt;
1 min read
HTML DOM console trace() Method
This console.trace() method is used to display the trace which represents how the code ended up at a certain point. Syntax: console.trace( label )Parameters: This method accepts a single parameter label. Example: In this example, we will use a console.trace() method C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;console.tra
1 min read
HTML DOM console table() Method
The console.table() method in HTML is used for writing data in tabular form in the console view. The table data is sent as a parameter to the console.table() method which must be an object or an array containing the data to be filled in the table. Syntax: console.table( tabledata, tablecolumns );Parameters: This method accepts two parameters as men
3 min read
HTML DOM console error() Method
The console.error() method in HTML is used to display an error message on the console. The console.error() method is used for testing purposes. The error message is sent as a parameter to the console.error() method. Syntax: console.error( message )Parameters: This method accepts a single parameter message which is mandatory and used to hold the err
1 min read
HTML DOM console count() Method
The console.count() method in HTML is used to write the number of times the console.count() method is called. The console.count() method can be added to a label that will be included in the console view. The label is an optional parameter sent to the console.count() method. Syntax: console.count( label )Parameters: This method accepts a single para
1 min read
HTML DOM console group() Method
The console.group() method in HTML is used to create a group of messages in the console. It indicates the start of a message group and all the messages written after calling the console.group() method will write inside the message group. The label is sent as an optional parameter to the console.group() method. Syntax: console.group( label )Paramete
2 min read
HTML DOM console groupEnd() Method
The console.groupEnd() method in HTML is used to indicate the end of a group of messages in the console which has been created using the console.group() method. This method does not accept any parameter. Syntax: console.groupEnd()Example: Below program illustrates the console.groupEnd() method in HTML C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt;
1 min read
HTML DOM console groupCollapsed() Method
The console.groupCollapsed() method in HTML is used to create a collapsed group of messages in the console. It indicates the start of a collapsed message group and all the messages written after calling the console.groupCollapsed() method will write inside the message group. The label is sent as an optional parameter to the console.groupCollapsed()
2 min read
Article Tags :