1. function setInterval(callback, interval) {
    2. let timer
    3. const now = Date.now
    4. let startTime = now()
    5. let endTime = startTime
    6. const loop = () => {
    7. timer = window.requestAnimationFrame(loop)
    8. endTime = now()
    9. if (endTime - startTime >= interval) {
    10. startTime = endTime = now()
    11. callback(timer)
    12. }
    13. }
    14. timer = window.requestAnimationFrame(loop)
    15. return timer
    16. }
    1. function clearInterval(timer) {
    2. cancelAnimationFrame(timer)
    3. }