Find elapsed time in javascript

前端 未结 9 556
死守一世寂寞
死守一世寂寞 2020-12-10 03:35

I\'m new to JavaScript and I\'m trying to write a code which calculates the time elapsed from the time a user logged in to the current time.

Here is my code:-

9条回答
  •  轮回少年
    2020-12-10 04:12

    We can also use console.time() and console.timeEnd() method for the same thing.

    Syntax:

    console.time(label);
    console.timeEnd(label);
    

    Label: The name to give the new timer. This will identify the timer; use the same name when calling console.timeEnd() to stop the timer and get the time output to the console.

    let promise = new Promise((resolve, reject) => setTimeout(resolve, 400, 'resolved'));
    
    // Start Timer
    console.time('x');
    
    promise.then((result) => {
      console.log(result);
    
      // End Timer
      console.timeEnd('x');
    });

提交回复
热议问题