美文网首页
使用requestAnimationFrame设置定时器

使用requestAnimationFrame设置定时器

作者: _嘿嘿_ | 来源:发表于2024-04-22 17:28 被阅读0次
    function rafSetTimeout(callback,timer) {
        let handle = {id:0}
        const start = Date.now();
        const loop = ()  => {
            const current = Date.now();
            if(current-start > timer){
                callback();
            }else{
                handle.id = requestAnimationFrame(loop);
            }
        }
        handle.id = requestAnimationFrame(loop);
        return handle;
    }

    function rafInterval(callback,timer) {
        let handle = {id:0}
        let start = Date.now();
        const loop = ()  => {
            const current = Date.now();
            if(current-start > timer){
                callback();
                start = Date.now();
            }
            handle.id = requestAnimationFrame(loop);
        }
        handle.id = requestAnimationFrame(loop);
        return handle;
    }


相关文章

网友评论

      本文标题:使用requestAnimationFrame设置定时器

      本文链接:https://www.haomeiwen.com/subject/vtcixjtx.html