setTimeout,setInterval,requestAnimationFrame动画精确度对比

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .progress {
            width: 0;
            height: 20px;
            line-height: 20px;
            background: yellowgreen;
        }
    </style>
</head>
<body>
    <div id="bar" class="progress">0%</div>
    <button id="btn">setTimeout</button>
    <button id="btn1">setInterval</button>
    <button id="btn2">requestAnimationFrame</button>

    <script>
        // 使用setTimeout,第一次执行完定时回调,全部执行完调用clearTimeout清除定时器
        btn.onclick = function(){
            console.time('setTimeout')
            var progress = 0
            var timer = setTimeout(function fn() {
                progress ++
                if(progress <= 100){
                    bar.style.width = progress + 'px'
                    bar.innerHTML = progress + '%'
                    timer = setTimeout(fn, 1000/60)
                }else{
                    console.timeEnd('setTimeout')
                    clearTimeout(timer)
                }    
            }, 1000/60)
        }
        
        // 使用setInterval,全部执行完调用clearInterval清除定时器
        btn1.onclick = function(){
            console.time('setInterval')
            var progress = 0
            var timer = setInterval(function fn() {
                progress ++
                if(progress <= 100){
                    bar.style.width = progress + 'px'
                    bar.innerHTML = progress + '%'
                }else{
                    console.timeEnd('setInterval')
                    clearInterval(timer)
                }    
            }, 1000/60)
        }

        // 使用requestAnimationFrame,用法与settimeout相似
        // 第一次执行完调用requestAnimationFrame,但不需要设置时间,全部执行完调用cancelAnimationFrame
        btn2.onclick = function(){
            console.time('requestAnimationFrame')
            var progress = 0
            var timer = requestAnimationFrame(function fn(){
                progress ++
                if(progress <= 100){
                    bar.style.width = progress + 'px'
                    bar.innerHTML = progress + '%'
                    timer = requestAnimationFrame(fn)
                }else{
                    console.timeEnd('requestAnimationFrame')
                    cancelAnimationFrame(timer)
                }    
            })
        }

        // 理论动画时长约1666ms,setTimeout和setInterval设置60fps,requestAnimationFrame默认
        // setTimeout: 1700.072021484375ms
        // setInterval: 1617.007080078125ms
        // requestAnimationFrame: 1664.283203125ms
        // 结论:setTimeout和setInterval动画执行误差约20%-30%,requestAnimationFrame误差约1%
    </script>
</body>
</html>

  

转载于:https://www.cnblogs.com/huangtonghui/p/10178068.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值