Date

  1. Date is a constructor whose prototype defines a number of Date methods

  2. Date()

    Perform Date(), returning a string representing the Date

  3. new Date()

    Returns the current date instance object with no arguments

    var date = new Date(a);console.log(Object.prototype.toString.call(date)); // [Object Date]
    date.getFullYear() // Return the year, such as 2020
    dae.getMonth() // Return month, subtract 1, add 1 to get month
    date.getDate() // Return the number from 1 to 31
    date.getDay() // Return the day of the week, 0 to 7, starting on Sunday
    date.getHours() // Return hour
    date.getMinutes() // Return minutes
    date.getSeconds() // Return the number of seconds
    date.getMilliseconds() // Return the number of milliseconds
    // There is also a set method
    Copy the code

    With parameters, return the corresponding date

    var date = new Date(2020.1.1.8.30.10);
    var date = new Date('2020/1/1 8:30:10');
    var date = new Date('2020/01/01 08:30:10');
    var date = new Date('the 2020-1-1 8:30:10');
    // Sat Feb 01 2020 8:30:10 GMT+0800
    Copy the code
  4. getTime()

    Returns a timestamp, which is the number of milliseconds

    Computer era time is 0, 0 minutes 0 seconds on January 1, 1970

    Timestamp: The number of milliseconds elapsed between a certain time and the computer epoch time

    var dateTime = new Date().getTime(); // Return the timestamp of the current time
    var date = new Date(dateTime); // Return the time corresponding to the timestamp
    date.setTime(dateTime); // Set the timestamp as the standard
    Copy the code

The timer

  1. setInterval

    The internal function is executed every specified number of milliseconds, starting from the current time

    Returns a number that uniquely identifies the timer and represents the sequence number in all timers and delayers

    Is a method under window

    setInterval(function(){},1000) // Anonymous function
    funtion Test(){}setInterval(test, 1000);
    setInterval('test()'.1000); // Pass the method as a string
    Copy the code
  2. clearInterval

    Cleanup timer

    var timer = setInterval(function(){}, 1000); // The timer is a unique identifier that represents the sequence number in all timers
    clearInterval(timer);
    Copy the code

decelerator

  1. setTimout

    Delay specifies the time to execute an internal function once

    Returns a number that uniquely identifies the delayer and represents the serial number of all timers and delayers

    setTimeout(function(){},1000) // Anonymous function
    funtion Test(){}setTimeout(test, 1000);
    Copy the code
  2. clearTimeout

    Clearance delay device

    var timer = setTimeout(function(){}, 1000); // The timer is a unique identifier that represents the sequence number in all timers
    clearTimeout(timer);
    Copy the code

Timing task

  1. function

    The task is executed when the condition is met. If the condition is not met within the set time, the callback function is executed

  2. parameter

    • Re: determine the condition function, return the condition to execute fn
    • Fn: The target function waiting to be executed
    • The time interval of space: setInterVal, space | | 100
    • Wait: setTimeOut waiting time, wait | | 3000
    • Back: callback function if fn fails to execute successfully
  3. code

    function timer(re, fn, space, wait, back) {
        if (re()) {
            fn();
        } else {
            var interval = setInterval(function() {
                if (re()) {
                    fn();
                    clearInterval(interval);
                    interval = null;
                }
            }, space || 100);
            setTimeout(function() {
                if (interval) {
                    clearInterval(interval);
                    interval = null;
                    back && back();
                }
            }, wait || 3000); }}Copy the code