|
|
@@ -41,130 +41,6 @@ export function parseTime (time, cFormat) {
|
|
|
return time_str
|
|
|
}
|
|
|
|
|
|
-export function formatTime (time, option) {
|
|
|
- if (('' + time).length === 10) {
|
|
|
- time = parseInt(time) * 1000
|
|
|
- } else {
|
|
|
- time = +time
|
|
|
- }
|
|
|
- const d = new Date(time)
|
|
|
- const now = Date.now()
|
|
|
-
|
|
|
- const diff = (now - d) / 1000
|
|
|
-
|
|
|
- if (diff < 30) {
|
|
|
- return '刚刚'
|
|
|
- } else if (diff < 3600) {
|
|
|
- // less 1 hour
|
|
|
- return Math.ceil(diff / 60) + '分钟前'
|
|
|
- } else if (diff < 3600 * 24) {
|
|
|
- return Math.ceil(diff / 3600) + '小时前'
|
|
|
- } else if (diff < 3600 * 24 * 2) {
|
|
|
- return '1天前'
|
|
|
- }
|
|
|
- if (option) {
|
|
|
- return parseTime(time, option)
|
|
|
- } else {
|
|
|
- return (
|
|
|
- d.getMonth() +
|
|
|
- 1 +
|
|
|
- '月' +
|
|
|
- d.getDate() +
|
|
|
- '日' +
|
|
|
- d.getHours() +
|
|
|
- '时' +
|
|
|
- d.getMinutes() +
|
|
|
- '分'
|
|
|
- )
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-export function debounce (func, wait, immediate) {
|
|
|
- let timeout, context, args
|
|
|
-
|
|
|
- const later = function () {
|
|
|
- if (!immediate) {
|
|
|
- func.apply(context, args)
|
|
|
- }
|
|
|
- timeout = context = args = null
|
|
|
- }
|
|
|
-
|
|
|
- return function (...params) {
|
|
|
- context = this
|
|
|
- args = params
|
|
|
- if (timeout) {
|
|
|
- clearTimeout(timeout)
|
|
|
- } else if (immediate) {
|
|
|
- func.apply(context, args)
|
|
|
- context = args = null
|
|
|
- }
|
|
|
- timeout = setTimeout(later, wait)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// leading-true,trailing-true:默认情况,间隔到了立即执行且保留尾调用
|
|
|
-// leading-true,trailing-false:去除尾调用
|
|
|
-// leading-false, trailing-true:在间隔结束时执行函数
|
|
|
-export function throttle (func, wait, options) {
|
|
|
- let timeout, context, args
|
|
|
- // 上一次执行回调的时间戳
|
|
|
- let previous = 0
|
|
|
-
|
|
|
- // 无传入参数时,初始化 options 为空对象
|
|
|
- if (!options) {
|
|
|
- options = {}
|
|
|
- }
|
|
|
-
|
|
|
- const later = function () {
|
|
|
- // 当设置 { leading: false } 时
|
|
|
- // 每次触发回调函数后设置 previous 为 0
|
|
|
- // 不然为当前时间
|
|
|
- previous = options.leading === false ? 0 : Date.now()
|
|
|
- func.apply(context, args)
|
|
|
- timeout = context = args = null
|
|
|
- }
|
|
|
-
|
|
|
- // 每次触发事件回调都执行这个函数
|
|
|
- // 函数内判断是否执行 func
|
|
|
- // func 才是我们业务层代码想要执行的函数
|
|
|
- return function (...params) {
|
|
|
- // 记录当前时间
|
|
|
- const now = Date.now()
|
|
|
-
|
|
|
- // 第一次执行时(此时 previous 为 0,之后为上一次时间戳)
|
|
|
- // 并且设置了 { leading: false }(表示第一次回调不执行)
|
|
|
- // 此时设置 previous 为当前值,表示刚执行过,本次就不执行了
|
|
|
- if (!previous && options.leading === false) {
|
|
|
- previous = now
|
|
|
- }
|
|
|
-
|
|
|
- // 距离下次触发 func 还需要等待的时间
|
|
|
- const remaining = wait - (now - previous)
|
|
|
- context = this
|
|
|
- args = params
|
|
|
-
|
|
|
- if (remaining <= 0) {
|
|
|
- if (timeout) {
|
|
|
- clearTimeout(timeout)
|
|
|
- timeout = null
|
|
|
- }
|
|
|
-
|
|
|
- // 设置 previous 为当前时间
|
|
|
- previous = now
|
|
|
-
|
|
|
- // 执行 func 函数
|
|
|
- func.apply(context, args)
|
|
|
- context = args = null
|
|
|
- } else if (!timeout && options.trailing !== false) {
|
|
|
- // 最后一次需要触发的情况
|
|
|
- // 如果已经存在一个定时器,则不会进入该 if 分支
|
|
|
- // 如果 {trailing: false},即最后一次不需要触发了,也不会进入这个分支
|
|
|
- // 间隔 remaining milliseconds 后触发 later 方法
|
|
|
- timeout = setTimeout(later, remaining)
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
export function createListOptions (params) {
|
|
|
return {
|
|
|
list: [],
|