index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * 判断数据是否为 type, 或返回 type
  3. * @param {*} data
  4. * @param {*} type
  5. * @returns
  6. */
  7. function isType(data, type = undefined) {
  8. const dataType = Object.prototype.toString
  9. .call(data)
  10. .match(/\s(.+)]/)[1]
  11. .toLowerCase()
  12. return type ? dataType === type.toLowerCase() : dataType
  13. }
  14. /**
  15. autoList 参考示例
  16. [
  17. {
  18. time: 1000 * 5, // 循环调用时间
  19. fn: this.currentTime, // 要调用的函数
  20. }
  21. ]
  22. **/
  23. function autoVueFn(autoList = [], opt) {
  24. if (!opt) {
  25. opt = {
  26. vm: this,
  27. }
  28. } else if (opt.$attrs) {
  29. opt = {
  30. vm: opt,
  31. }
  32. }
  33. const { vm, batEnd } = opt
  34. let end = 0
  35. vm.timer_ = []
  36. autoList.forEach(async (item) => {
  37. const fn = async () => {
  38. await item.fn.call(vm)
  39. end = end + 1
  40. if (end >= autoList.length) {
  41. batEnd && batEnd.call(vm)
  42. }
  43. }
  44. fn()
  45. const timer = setInterval(fn, item.time)
  46. vm.timer_.push(timer)
  47. })
  48. vm.$options.beforeDestroy = [
  49. ...(vm.$options.beforeDestroy || []),
  50. function () {
  51. vm.timer_.forEach((item) => {
  52. console.log(`卸载`, item)
  53. clearInterval(item)
  54. })
  55. },
  56. ]
  57. }
  58. export default {
  59. autoVueFn,
  60. isType,
  61. }