sf-utils2 sf-utils2
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
版本v3.3.3-beta1
首页
  • 01.快速开始 🔥
  • 02.基础-Base
  • 03.对象-Object
  • 04.数组-Array
  • 05.方法-Function
  • 06.字符串-String
  • 07.数学-Math
  • 08.dom
  • 09.拓展
  • webpack5.x教程学习 (opens new window)
  • 例子
  • 教程 🔥
  • 例子配置
企业级后台模版 (opens new window)
  • 快速开始

  • 基础-Base

  • 对象-Object

  • 数组-Array✨✨✨

  • 方法-Function

  • 字符串-String

  • 数学-Math

  • 文件-Buffer

  • 节点-dom

    • 序言 👏
    • _constant
    • getHttpBlob【获取远程二进制数据流】
    • downloadFile【http下载文件】
    • loadJsSync【单个加载js文件】
    • loadJsOrCssMulSync【批量加载远程css和js文件】
    • getAbsOffsetTop【距离父滚动区域顶部绝对距离】 🔥
    • getParentScrollElement【获取父元素滚动元素】 🔥
    • isScroll【是否是滚动容器】
    • scrollToX【水平滚动到某个具体位置】 🔥
    • scrollToY【垂直滚动到某个具体位置】 🔥
    • scrollToElement【滚动到某个dom元素】 🔥
    • isElementInContainer【元素是否整体在容器内】
    • batchElsPosInContainer【批量 dom 元素在 父的滚动视图位置】 🔥
    • isElementVisibleInViewport【是否可见在某个股东元素上】 🔥
    • observerElementMutation【监听dom元素属性变化】 🔥
    • print【打印】 🔥
    • partialCb【分片加载】
      • 1.示例
      • 2.入参说明
        • 主入参
      • 3.源码
    • domUtils【dom方法操作基础】 🔥
  • 拓展

  • nodejs

目录

partialCb【分片加载】

描述

分片加载,适用于一次性渲染大量数据

# 1.示例

插入10w条数据 总用时:0ms
<template>
  <div>
    <el-button @click="onInsertData">插入10w条数据</el-button>
    <span>总用时:{{ time }}ms</span>
    <div
      v-loading="loading"
      id="xk8son2p"
      class="mt-[10px] h-[300px] p-[12px] border-1px overflow-y-auto overflow-x-hidden"
      ref="container"
    ></div>
  </div>
</template>

<script>
import { partialCb } from 'sf-utils2'

export default {
  data() {
    return {
      time: 0,
      loading: false
    }
  },
  mounted() {
    console.log(this)
  },

  methods: {
    onInsertData() {
      console.time('计时')
      const start = Date.now()
      // const list = [...new Array(10 ** 6)].map((v, vi) => ({ index: vi + 1 }))

      const list = Array.from(new Array(10 ** 5)).map((v, vi) => ({ index: vi + 1 }))

      const fragment = document.createDocumentFragment()

      // 每次回调方法
      const callbackFn = partialData => {
        for (let i = 0; i < partialData.length; i++) {
          const div = document.createElement('div')
          // console.log(i)
          div.innerHTML = partialData[i].index
          div.style.cssText = `height: 30px; display: flex; align-items: center; justify-content: center;margin-top: 10px; border: 1px solid #eee; border-radius: 4px`
          fragment.append(div)
        }
      }

      // 所有都执行结束了,回调执行的方法
      const endCallBackFn = () => {
        this.$message.success('执行结束')
        console.timeEnd('计时')
        this.$refs.container.append(fragment)
        this.time = Date.now() - start
      }
      partialCb({
        data: list,
        beforeCb: () => {
          this.loading = true
        },
        cb: callbackFn,
        afterCb: () => {
          this.loading = false
          endCallBackFn()
        },
        loopLength: 10 ** 3 // 每次切片的大小长度
      })
    }
  }
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
显示代码 复制代码 复制代码

# 2.入参说明

# 主入参

参数 说明 类型 是否必填 默认值
data 数据 Array 是 -
beforeCb 运行之前回调函数 Function - -
loopLength loopLength 每次分片的大小 Number - -
cb 运行每次分片回调函数 Function - -
afterCb 运行结束回调函数 Function - -

# 3.源码

源码,点开查看 👈
/**
 * 分片加载
 * @param {Array} data 数据
 * @param {Function} beforeCb 开始之前执行
 * @param {Function} cb 回调函数
 * @param {Function} afterCb 结束后回调
 * @param {Number} loopLength 每次分片的大小
 * @param {Function} afterCb 运行结束回掉函数
 */
import { _requestAnimationFrame } from './_constant'
import isPromise from '@/base/isPromise'

function partialCb({ data = [], beforeCb, cb = new Function(), loopLength = 100, afterCb }) {
  const totalLoop = Math.ceil(data.length / loopLength)
  const _context = this
  let partialIndex = 0
  beforeCb && beforeCb()
  async function loadPartCb(_data) {
    let partialData = _data.slice(partialIndex * loopLength, loopLength * (partialIndex + 1))
    const p = cb(partialData, partialIndex, _data)
    if (isPromise(p)) await p
    if (partialIndex < totalLoop) {
      partialIndex++
      _requestAnimationFrame(loadPartCb.bind(_context, _data))
    } else {
      afterCb && afterCb()
    }
  }
  _requestAnimationFrame(loadPartCb.bind(_context, data))
}

export default partialCb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
上次更新: 2025/07/01, 14:52:29
print【打印】 🔥
domUtils【dom方法操作基础】 🔥

← print【打印】 🔥 domUtils【dom方法操作基础】 🔥→

Theme by Vdoing | Copyright © 2022-2025 bianpengfei
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×