推荐关注
› Meteor
› JSLint - a JavaScript code quality tool
› jsFiddle
› D3.js
› WebStorm
推荐书目
› JavaScript 权威指南第 5 版
› Closure: The Definitive Guide
Tlin

定时器怎么优化提升页面性能?

  •  
  •   Tlin · May 11, 2020 · 3397 views
    This topic created in 2370 days ago, the information mentioned may be changed or developed.

    写了一个定时器获取本地时间显示在页面上( setInterval ),但是切换到其它页面或者缩小挂后台,等一段时间后就很卡。用了 chrome 自带的 perforance 查看,蓝色线条逐步上升,内存应该泄漏了。 一般这种你们怎么优化呢?网上搜了一些,感觉都没有具体怎么做。

    6 replies  •  2020-05-12 20:11:53 +08:00
    rabbbit
    rabbbit
    May 11, 2020
    1
    一般用 setTimeout 代替 setInterval

    function sInterval( fn, time) {
      function callback() {
       setTimeout(function () {
        fn();
        callback()
      }, time)
     }
      callback();
    }
    SilentDepth
    SilentDepth
    May 12, 2020
    2
    用 requestAnimationFrame
    Tlin
    Tlin
    OP
    May 12, 2020
    3
    @rabbbit @SilentDepth 试了,还不错的。谢谢了。但是用 requestAnimationFrame 过段时间后存在动画卡住。就是读秒会不动,但是分以上的部分还是会动的。有些无解
    SilentDepth
    SilentDepth
    May 12, 2020
    4
    @Tlin #3 秒不动分动?诡异。你发代码出来看看
    Tlin
    Tlin
    OP
    May 12, 2020
    5
    @SilentDepth 刚吃好饭。因为我使用 css3 的翻转等动画,造成动画卡死,时分秒都会卡。如果仅是获取时分秒,不做特效就行。这点我需要优化一下
    SilentDepth
    SilentDepth
    May 12, 2020
    6
    @Tlin #5 动画也用 rAF 触发或计算就行了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1756 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 04:12 · PVG 12:12 · LAX 20:12 · JFK 23:12
    ♥ Do have faith in what you're doing.