js requestAnimationFrame 每秒执行一次

let count = 0, maxCount = 3;
function event() {
  count += 1;
  console.log(count);

  return count < maxCount;
}

let prev = 0;
const t = 1000; // 1秒
function anime(timestamp) {
  const elapsed = timestamp - prev;
  if (elapsed > t) {
    if (!event()) return;
    prev = timestamp;
  }
  requestAnimationFrame(anime);
}

requestAnimationFrame(anime);
posted @ 2022-08-17 10:08  Ajanuw  阅读(337)  评论(0编辑  收藏  举报