setTimeout setInterval 性能比较

2019-01-24  本文已影响0人  AAA前端

前面有时候在做重复调用时,我试过在回调函数中再次使用setTimeout调用函数,以达到setInterval的效果。虽然感觉区别不大。今天闲着自己试验了一下。

<!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>
        span {
            position: absolute;
            left: 0;
            top: 10%;
        }
    </style>
</head>
<span id="box">哈哈</span>

<body>
    <script src="https://cdn.bootcss.com/zepto/1.0rc1/zepto.min.js"></script>
    <script>
        var count = 10;
        var startTime = new Date();
        var $box = $('#box');

        function move() {
            if ($box.css('left').slice(0, -2) - 0 < 200) {
                $box.css('left', count + "px")
                count = count + 1;
                // setTimeout(move, 10)
                // requestAnimationFrame(move)
            } else {
                var now = new Date() - startTime;
                console.log(now)  // 1961
                clearInterval(timer)
            }
        }

        // setTimeout(move, 10)
        // requestAnimationFrame(move)
        var timer = setInterval(move, 10)
    </script>
</body>
</html>

<!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>
        span {
            position: absolute;
            left: 0;
            top: 10%;
        }
    </style>
</head>
<span id="box">哈哈</span>

<body>
    <script src="https://cdn.bootcss.com/zepto/1.0rc1/zepto.min.js"></script>
    <script>
        var count = 10;
        var startTime = new Date();
        var $box = $('#box');

        function move() {
            if ($box.css('left').slice(0, -2) - 0 < 200) {
                $box.css('left', count + "px")
                count = count + 1;
                setTimeout(move, 10)
                // requestAnimationFrame(move)
            } else {
                var now = new Date() - startTime;
                console.log(now)  // 2151
                // clearInterval(timer)
            }
        }

        setTimeout(move, 10)
        // requestAnimationFrame(move)
        // var timer = setInterval(move, 10)
    </script>
</body>
</html>
<!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>
        span {
            position: absolute;
            left: 0;
            top: 10%;
        }
    </style>
</head>
<span id="box">哈哈</span>

<body>
    <script src="https://cdn.bootcss.com/zepto/1.0rc1/zepto.min.js"></script>
    <script>
        var count = 10;
        var startTime = new Date();
        var $box = $('#box');

        function move() {
            if ($box.css('left').slice(0, -2) - 0 < 200) {
                $box.css('left', count + "px")
                count = count + 1;
                // setTimeout(move, 10)
                requestAnimationFrame(move)
            } else {
                var now = new Date() - startTime;
                console.log(now)  // 3623
                // clearInterval(timer)
            }
        }

        // setTimeout(move, 10)
        requestAnimationFrame(move)
        // var timer = setInterval(move, 10)
    </script>
</body>
</html>

*最后虽然是时间长了,但是动画变得流畅了。

个人总结来说,动画的话推荐用requestAnimationFrame。如果不是动画,是一定频率的算法等优先setInterval. 延迟执行的话用setTimeout;

上一篇 下一篇

猜你喜欢

热点阅读