1. window.requestAnimationFrame(callback);

    告诉浏览器——你希望执行一个动画,并且要求浏览器在下次重绘之前调用指定的回调函数更新动画。该方法需要传入一个回调函数作为参数,该回调函数会在浏览器下一次重绘之前执行

    1. <template>
    2. <div>
    3. <div v-for="(item, i) in list" :key="i"> {{i + 1}}---渲染大量数据</div>
    4. </div>
    5. </template>
    6. <script>
    7. export default {
    8. name: 'CliVue2Zz',
    9. data() {
    10. return {
    11. list: []
    12. };
    13. },
    14. mounted() {
    15. this.renderItem(10000)
    16. },
    17. methods: {
    18. renderItem(total) {
    19. let count = total / 10
    20. let animationId = ''
    21. console.log('00000',count)
    22. const showDiv = () => {
    23. console.log('111111')
    24. if (count < 0) {
    25. // 取消回调函数。
    26. window.cancelAnimationFrame(animationId)
    27. return
    28. }
    29. const currentList = new Array(10).fill(10)
    30. this.list = [...this.list, ...currentList]
    31. count--
    32. // 返回一个 long 整数,请求 ID
    33. animationId = window.requestAnimationFrame(showDiv)
    34. }
    35. showDiv()
    36. }
    37. },
    38. };
    39. </script>
    40. <style lang="scss" scoped>
    41. </style>