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

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

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

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

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

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