Open In App

Underscore.js _.now() function

Last Updated : 14 Jul, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

Underscore.js is a library in javascript that makes operations on arrays, string, objects much easier and handy. _.now() function is used to return the timestamp of the current time. This method can be useful when working with animations in the browser. 

Syntax:

_.now();

Parameters: It takes no parameters.

Returns: The return type is number.

Note: It is very necessary to link the underscore CDN before going and using underscore functions in the browser. When linking the underscore.js CDN The "_" is attached to the browser as a global variable.

Few examples are given below for a better understanding of the function.

Example 1:

html
<!DOCTYPE html> 
<html> 
  <head> 
    <script src =  
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > 
    </script> 
   </head> 
  <body>
    <script>
      console.log(`Current timestamp is: ${_.now()}`)
    </script>
  </body> 
</html>  

Output:

Example 2:

javascript
<!DOCTYPE html> 
<html> 
  <head> 
    <script src =  
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" > 
    </script> 
   </head> 
  <body>
    <script>
    //creating print function to print timestamp after delay of 10ms
      function print(){
        setTimeout(()=>{
          console.log(`Current timestamp is: ${_.now()}`)
        }, 10)
      }
      //running this function 5 times using _.times() function.
      _.times(5, print)
      console.log("Type of timestamp is: ", typeof(_.now()))
    </script>
  </body> 
</html>  

Output:


Next Article

Similar Reads