Skip to content

定时器实现

借助requestAnimationFrame实现,精度更高

mySetTimeout

js
function mySetTimeout(callback, delay) {
    let timer, start = Date.now()
    const loop = () => {
        if (start + delay > Date.now()) {
            timer = requestAnimationFrame(loop)
        } else {
            callback(timer)
        }
    }
    loop()
    return timer
}

mySetInterval

js
function mySetInterval(callback, delay) {
    let timer, start = Date.now()
    const loop = () => {
        if (start + delay <= Date.now()) {
            callback(timer)
            start = Date.now()
        }
        timer = requestAnimationFrame(loop)
    }
    loop()
    return timer
}