[JS] Console.time ()-timer constructor and how to timing

Source: Internet
Author: User

Overview

Use a timer to speed up the process of running your code. You can give each timer a name that can run up to 10,000 timers on each page. When you call the Console.timeend () function with a timer name, the browser returns a millisecond value that indicates the time that the timer started when you called Console.timeend ().

Grammar
Console.time (Timername);

Timername

The timer name, which is used to identify a timer that stops the corresponding timer when it is called with the name Console.timeend () and outputs the timing time in the console.

How to capture a timer return value

Unfortunately, Console.time () and Console.timeend () can only output timings in the console, but cannot return the output, nor can it be assigned to a variable to be saved.

If you need to use timing as a variable, you can use the Window.performance.now () function to timing:

var start = window.performance.now (); var end = Window.performance.now (); var duration = End-start;

  

Window.performance.now () returns a floating-point value that indicates the millisecond of the current distance when the page was loaded, and if you want to know when the page was loaded, you can get the Window.performance.timing.navigationStart value That represents the Unix timestamp when the page was loaded.

You can also use the Date.now () function to clock, which returns an integer millisecond value.

var start = date.now (); var duration = Date.now ()-Start;

or the date (). GetTime () object, which returns a Unix timestamp:

var New Date (). GetTime (); var New Date (). GetTime (); var duration = End-start

PS:window.performance.now () is much slower than Date.now ().

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.