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

Source: Internet
Author: User
Tags date constructor end return variable window

Overview

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


Grammar

Console.time (Timername);



Timername

A timer name used to identify a timer that, when invoked with the name Console.timeend (), stops the timer and prints the timer time on the console.


How to capture the return value of a timer

Unfortunately, Console.time () and Console.timeend () can only output the timer at the console, but not the output, and cannot be assigned to the variable save.

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

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



Window.performance.now () returns a floating-point value indicating the millisecond time at which 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 is loaded.



You can also use the Date.now () function to time the function to return an integer millisecond value.

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



or a date (). GetTime () object that returns a Unix timestamp:

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



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.