Front-end development - js common tool functions (utilities)

1. Time

function getCurTime() {
    var date = new Date();
    return date.toLocaleTimeString();
}
  • date.toLocaleTimeString(): am or afternoon, what time and what time;
  • date.toLocaleDateString(): date, month and day;

2. Random

  • Interval:

    function randInt(low, high) {
        return Math.floor(Math.random()*(high-low) + low)
    }

3. setInterval 与 setTimeout

  • Execute every 1 second:

    setInterval ( function () {
        console.log('hello world')
    }, 1000)
    setInterval ( function () {
        console.log(Math.floor(Math.random()))
    }, 1000)

    How to close the timing cycle execution, you need to save the id identification code in advance:

    var id = setInterval ( function () {
        var num = Math.floor(Math.random() * 1024)
        console.log(num)
    }, 3000);
    ....
    clearInterval(id)
  • setTimeout() : Run the function regularly, it will only run once when the specified time is up;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326079581&siteId=291194637