util.js 860 B

123456789101112131415161718192021222324252627282930313233343536
  1. const formatTime = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  9. }
  10. const formatNumber = n => {
  11. n = n.toString()
  12. return n[1] ? n : '0' + n
  13. }
  14. function throttle(fn, gapTime) {//函数节流
  15. if (gapTime == null || gapTime == undefined) {
  16. gapTime = 1500
  17. }
  18. let _lastTime = null
  19. return function () {
  20. let _nowTime = + new Date()
  21. if (_nowTime - _lastTime > gapTime || !_lastTime) {
  22. // 将this和参数传给原函数
  23. fn.apply(this, arguments)
  24. _lastTime = _nowTime
  25. }
  26. }
  27. }
  28. module.exports = {
  29. formatTime: formatTime,
  30. throttle: throttle
  31. }