ソースを参照

refactor: replace requestAnimationFrame with setInterval in memory test

alvarosabu 5 ヶ月 前
コミット
5fcd0bcda7
1 ファイル変更6 行追加8 行削除
  1. 6 8
      playground/vue/src/pages/advanced/MemoryTresObjects.vue

+ 6 - 8
playground/vue/src/pages/advanced/MemoryTresObjects.vue

@@ -12,7 +12,7 @@ const msg = ref('Click Start Test to begin.')
 const r = ref(null)
 const isStarted = ref(false)
 
-let rafId: number
+let intervalId: ReturnType<typeof setInterval>
 
 const startTest = () => {
   isStarted.value = true
@@ -20,7 +20,7 @@ const startTest = () => {
   msg.value = 'Test is running...'
   show.value = true // Start by showing the canvas
 
-  const tick = () => {
+  intervalId = setInterval(() => {
     if (toggleCount.value < toggleMax) {
       if (r.value && show.value) {
         show.value = false
@@ -29,20 +29,18 @@ const startTest = () => {
       else if (!show.value) {
         show.value = true
       }
-      rafId = requestAnimationFrame(tick)
     }
     else {
+      clearInterval(intervalId)
       const elapsedSec = (Date.now() - startTimeMS.value) / 1000
       msg.value = `Test completed in ${elapsedSec} seconds.`
     }
-  }
-
-  rafId = requestAnimationFrame(tick)
+  }, 1000 / 120)
 }
 
 onUnmounted(() => {
-  if (rafId) {
-    cancelAnimationFrame(rafId)
+  if (intervalId) {
+    clearInterval(intervalId)
   }
 })
 </script>