zoukankan      html  css  js  c++  java
  • 前端开发 —— js 常用工具函数(utilities)

    1. 时间

    function getCurTime() {
        var date = new Date();
        return date.toLocaleTimeString();
    }
    • date.toLocaleTimeString():上午还是下午,几点几分;
    • date.toLocaleDateString():日期,月和日;

    2. 随机

    • 区间:

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

    3. setInterval 与 setTimeout

    • 每隔 1 秒执行一次:

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

      如何关闭定时周期执行呢,需要事先保存 id 识别码:

      var id = setInterval ( function () {
          var num = Math.floor(Math.random() * 1024)
          console.log(num)
      }, 3000);
      ....
      clearInterval(id)
    • setTimeout() :定时运行函数,只会在指定时间到时运行一次;

  • 相关阅读:
    HDU 2865 Birthday Toy
    POJ 2888 Magic Bracelet
    BZOJ 3105 新Nim游戏
    BZOJ 2916 Monochromatic Triangles
    Countries
    Memory and Scores
    Paint on a Wall
    这是一道智障题
    可持久化数据结构
    数一的逆袭
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9420987.html
Copyright © 2011-2022 走看看