const rafInterval = (fn, delay = 16, count) => { let lastRunTime let timerId = null let runCount = 0 const loop = (timestamp) => { if (lastRunTime === undefined) { lastRunTime = timestamp } timerId = requestAnimationFrame(loop) if (timestamp - lastRunTime >= delay) { if (count && runCount >= count) { cancelAnimationFrame(timerId) } else { runCount++ fn(timestamp) lastRunTime = timestamp } } } timerId = requestAnimationFrame(loop) return timerId}let idlet count = 0let b = (t) => { console.log(count, t) count++ id = rafInterval(b, 2000, 1) if (count > 8) { window.cancelAnimationFrame(id) }}b()