美文网首页
微信小程序:js工具函数

微信小程序:js工具函数

作者: 我的小小笔尖 | 来源:发表于2022-10-20 13:07 被阅读0次

存档,备用

// 节流:在一定时间内连续触发某事件,在这段时间段内只执行首次触发的那一次。
function throttleFunc(func, timeGap) {    
  if (timeGap==null || typeof(timeGap)=="undefined") {
    timeGap = 2000 // 如果没有传递参数timeGap,或者该参数值为空,则使用默认值2000毫秒
  }
  let lastTime = null
  return function () {
    let currentTime = + new Date()
    if (currentTime - lastTime > timeGap || !lastTime) {
      console.log('exec', currentTime) // 正式环境可去掉
      func.apply(this, arguments)
      lastTime = currentTime
    }else {
      console.log('no exec') // 正式环境可去掉
    }
  }
}

// 格式化日期,示例:2021/12/01 12:12:01
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

// 格式化日期,示例:20211201121201
const formatTime2 = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('')}${[hour, minute, second].map(formatNumber).join('')}`
}

// 格式化日期,示例:2021/12/01
const formatTime3 = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return `${[year, month, day].map(formatNumber).join('-')}`
}

// 数字不足两位时,前面补0
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

// 数字不足三位时,后面补0
const formatNumber2 = n => {
  n = n.toString()
  return n[2] ? n :  n[1] ? `${n}0` : `${n}00`
}

// 格式化时间,格式化示例:05:00
const formatShowTime = number => {
  let minute = Math.floor(number/60)
  let second = Math.floor(number%60)

  return `${[minute, second].map(formatNumber).join(':')}`
}

// 格式化时间,格式化示例:05:00.000
const formatShowMillisecond = number => {
  let minute = Math.floor(Math.floor(number/1000)/60)
  let second = Math.floor(Math.floor(number/1000)%60)
  let millisecond = Math.floor(number%1000)

  return `${[minute, second].map(formatNumber).join(':')}` + '.'+formatNumber2(millisecond)
}

module.exports = {
  throttleFunc,
  formatTime,
  formatTime2,
  formatTime3,
  formatShowTime,
  formatShowMillisecond,
}

相关文章

  • 微信小程序开发笔记

    小程序的代码结构 文件类型 文件夹 pages用于存放页面文件utils.js工具函数 基本结构 微信小程序开发框...

  • 小程序源码 百度云资源

    微信小程序最近火的不要不要的,站长素材下载微信小程序的开发工具试了一下, 发现只要会css+js+熟悉微信小程序a...

  • 微信小程序基础

    微信小程序介绍微信小程序开发工具的使用微信小程序框架文件微信小程序逻辑层微信小程序视图层微信小程序组件介绍微信小程...

  • Js知识点及刷题记录

    知识点:匿名函数,函数作用域,异步执行的理解 一,微信小程序中page页面里面的函数定义与js的差异js中: 而p...

  • 微信小程序

    微信小程序 基础技术设备 开发者工具的使用 框架全局文件 App.js小程序逻辑App.js文件用来定义全局数据和...

  • 微信小程序 助你爬坑

    目录 已更新 微信小程序 开发简介微信小程序 账号注册及开发工具下载微信小程序 目录结构介绍微信小程序 获取App...

  • 微信小程序第二弹,页面组成

    微信小程序页面组成结构每一个页面由上面四个文件组成.js文件,各种函数,就和网页的js类似,不过小程序的js文件不...

  • 微信小程序

    微信公众平台 到 微信小程序开发 到微信小程序文档。微信web开发者工具

  • 微信小程序如何更改文本?

    微信小程序更改文本 wxml: js:

  • 微信小程序之注册配置

    微信小程序之注册配置 小程序页面结构 pagespages/app.js(小程序注册入口)pages/app.js...

网友评论

      本文标题:微信小程序:js工具函数

      本文链接:https://www.haomeiwen.com/subject/klqmzrtx.html