function animate() {
var start = +new Date(); // 幀開始時間戳
// ... 動畫邏輯執行 ...
var finish = +new Date(); // 幀結束時間戳
setTimeout(animate, 1000 - (finish - start)); // 下一幀延遲 = 目標週期 - 耗時
}
function animate(time) {
// time 為瀏覽器提供的高精度時間戳 (performance.now())
// ... 動畫邏輯 ...
requestAnimationFrame(animate); // 瀏覽器自動對齊螢幕重繪率 (通常 60fps)
}
requestAnimationFrame(animate);
教學代碼展示**補償邏輯**概念,但實務開發**請直接使用 `requestAnimationFrame`**,更簡潔、高效、省電。