Skip to content

Commit

Permalink
Merge pull request #593 from fsih/fixWaitCalledEveryFrame
Browse files Browse the repository at this point in the history
Change wait to use a promise instead of being called every frame
  • Loading branch information
fsih committed Jun 8, 2017
2 parents d41997b + e237d24 commit fb986ef
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/blocks/scratch3_control.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const Cast = require('../util/cast');
const Timer = require('../util/timer');

class Scratch3ControlBlocks {
constructor (runtime) {
Expand Down Expand Up @@ -73,18 +72,13 @@ class Scratch3ControlBlocks {
util.startBranch(1, true);
}

wait (args, util) {
if (!util.stackFrame.timer) {
util.stackFrame.timer = new Timer();
util.stackFrame.timer.start();
util.yield();
this.runtime.requestRedraw();
} else {
const duration = Math.max(0, 1000 * Cast.toNumber(args.DURATION));
if (util.stackFrame.timer.timeElapsed() < duration) {
util.yield();
}
}
wait (args) {
const duration = Math.max(0, 1000 * Cast.toNumber(args.DURATION));
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, duration);
});
}

if (args, util) {
Expand Down

0 comments on commit fb986ef

Please sign in to comment.