From 9de4811adfa287d0e081c1337e26597c6d898859 Mon Sep 17 00:00:00 2001 From: Victor Westerlund Date: Thu, 11 Nov 2021 15:36:48 +0100 Subject: [PATCH] dev21w45-a --- monkey/Monkey.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/monkey/Monkey.js b/monkey/Monkey.js index 8d0bd09..eb15276 100644 --- a/monkey/Monkey.js +++ b/monkey/Monkey.js @@ -17,32 +17,31 @@ class Monkey { // Task scheduler next() { - if(this.flags[0] === 0 || this.flags[2] === 0) return this.abort(); - const task = this.tasks[this.i]; + const start = performance.now(); + const self = this; + let task = null; // Run task after delay - this.queue.thisTask = setTimeout(() => { - // Dispatch task to main thread + function frame(time) { + if(self.flags[0] === 0 || self.flags[2] === 0) return self.abort(); postMessage(["TASK",task]); - this.i++; - },task[0]); - - // Loop until flag is 0 or infinite if 255 - if(this.i === this.tasksLength) { - this.i = -1; - if(this.flags[1] < 255) this.flags[2]--; + self.i++; + scheduleFrame(time); } // Queue the next task - this.queue.nextTask = setTimeout(() => this.next(),task[0]); + function scheduleFrame(time) { + task = self.tasks[self.i]; + //const elapsed = Math.round(performance.now() - start); + const wait = task[0] + start; + setTimeout(() => requestAnimationFrame(frame),wait); + } + + scheduleFrame(start); } abort() { this.flags[2] = 0; // Playing: false - clearTimeout(this.queue.thisTask); - clearTimeout(this.queue.nextTask); - this.queue.thisTask = null; - this.queue.nextTask = null; } // Set or get a runtime flag