1. const rafInterval = (fn, delay = 16, count) => {
    2. let lastRunTime
    3. let timerId = null
    4. let runCount = 0
    5. const loop = (timestamp) => {
    6. if (lastRunTime === undefined) {
    7. lastRunTime = timestamp
    8. }
    9. timerId = requestAnimationFrame(loop)
    10. if (timestamp - lastRunTime >= delay) {
    11. if (count && runCount >= count) {
    12. cancelAnimationFrame(timerId)
    13. } else {
    14. runCount++
    15. fn(timestamp)
    16. lastRunTime = timestamp
    17. }
    18. }
    19. }
    20. timerId = requestAnimationFrame(loop)
    21. return timerId
    22. }
    23. let id
    24. let count = 0
    25. let b = (t) => {
    26. console.log(count, t)
    27. count++
    28. id = rafInterval(b, 2000, 1)
    29. if (count > 8) {
    30. window.cancelAnimationFrame(id)
    31. }
    32. }
    33. b()