[Javascript] 渲染优化,延迟操作/hover太慢/循环的settimeout和清除

第一种情况,分页的table数据太长的话,用settimeout来push,每120ms就push一条数据

 /*  first page lazy load,
        add a item at each 120ms until it reaches the current pagelenght.*/
        const cur = this;
        for (let i = 0; i < this.currentComponent.TablePageLength && i < result.Rows.length; i++) {
          const item = JSON.parse(JSON.stringify(result.Rows[i]));
           setTimeout(() => {
            cur.firstInitPage.push(item);
            if (i === this.currentComponent.TablePageLength - 1 || i > 20 || i > result.Rows.length - 2) {
              cur.table.loadingStatus = true;
            }
          }, 120 * (i + 1));
        }

第二种,hover的时候,出现的div渲染太多导致hover太慢又不能分页

思路:鼠标hover的时候只渲染n条,滚动(不是scoll事件而是wheel事件)的时候重新加载并延迟,hover移出的时候恢复n条并且清除settimeout

清除的时候,用一个数组保存每个timer然后一一清除

 <div class=" btn-before option-data" 
    (mouseenter)="storeMouseenter()"
    (mouseleave)="storeLeave()"
    id="p-store" >
        <ul 
        id="scrollid"
        (mousewheel)="onScroll()"
        class="option-list mallcss">
            <ng-container 
            *ngFor="let item of
            (this.searchkey === ''?this.tempData:searchitemList);
            let i = index">
                <li class="checkbox-wrapper"
                 {....当又2000+条的时候}
                </li>
            </ng-container>
        </ul>
     
mainDiv = null;
  tempData = [];
  storeMouseenter() {

    this.mainDiv = document.getElementById('scrollid');
    this.mainDiv.scrollTop = 0;
    this.scollFlage = true;
  }

  storeLeave() {
    this.clearTimer();
    this.tempData = this.siteConfig.storeList.slice(0, 13);


  }
  clearTimer() {
    for (let i = 0; i < this.tuttimer.length; i++) {
      clearTimeout(this.tuttimer[i]);
    }
    this.tuttimer = [];
  }

  scollFlage = true;// only trigger once
  tuttimer = [];
  onScroll() {
    if (this.scollFlage) {
      this.scollFlage = false;
      const cur = this;
      for (let i = 0, j = 14; i < this.siteConfig.storeList.length / 10; i++, j = j + 10) {
        const timer = setTimeout(() => {
          cur.tempData.push(...this.siteConfig.storeList.slice(j, j + 10));
        }, 100 * (i + 1));
        this.tuttimer.push(timer);
      }
    }
  }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值