Wait Time between Blocks

110 views
Skip to first unread message

hyun lee

unread,
Nov 5, 2020, 12:52:10 AM11/5/20
to Blockly
Hello, first of all, thanks for all your help. I had another question about waiting time between blocks.

So I have this code:

    function runCode() {
      // Generate JavaScript code and run it.

      window.LoopTrap = 1000;
      Blockly.JavaScript.STATEMENT_PREFIX = 'highlightBlock(%1);\n';
      Blockly.JavaScript.addReservedWords('code');

      
      var code = Blockly.JavaScript.workspaceToCode(workspace);
      
      Blockly.JavaScript.INFINITE_LOOP_TRAP =
          'if (--window.LoopTrap == 0) throw "Infinite loop.";\n';
      Blockly.JavaScript.INFINITE_LOOP_TRAP = null;

      var myInterpreter = new Interpreter(code);
      nextStep();   
      
      try {
        eval(code);
      } catch (e) {
        swal(e);
      }
};


and my nextstep() is defined as

    function nextStep() {
      if (myInterpreter.step()) {
        window.setTimeout(nextStep, 5000);
      }
    };


I want to have 5 seconds pause between executing each block. However, when I run this code, it gives me this error:

Uncaught SyntaxError: Unexpected token (2:49)
    at c (acorn_interpreter.js:2)
    at N (acorn_interpreter.js:16)
    at v (acorn_interpreter.js:15)
    at ha (acorn_interpreter.js:24)
    at ha (acorn_interpreter.js:25)
    at Sa (acorn_interpreter.js:24)
    at Qa (acorn_interpreter.js:22)
    at A (acorn_interpreter.js:22)
    at F (acorn_interpreter.js:20)
    at Object.a.parse (acorn_interpreter.js:29)


Can somebody help me with this? Thanks a lot!




HABIB LEILA

unread,
Jul 15, 2021, 8:36:08 PM7/15/21
to Blockly
i'm facing same problem did you find any solution ?

MickeyMouse2918

unread,
Jul 19, 2021, 4:59:55 AM7/19/21
to Blockly
I am also facing this as well as some other weird behavior:

By just doing these lines, the interpreter automatically runs:

      var code = Blockly.JavaScript.workspaceToCode(Blockly.mainWorkspace);
      var myInterpreter = new Interpreter(code);

This causes the "nextStep()" function to not have any effect since the interpreter has already run the code. Any solution to that?

Educar br

unread,
Jul 8, 2024, 11:13:18 AMJul 8
to Blockly
 const runCode = () => {
    const code = javascriptGenerator.workspaceToCode(ws);
    console.log(code)
    const myInterpreter = new Interpreter(code, (interpreter, scope) => {
      interpreter.setProperty(scope, 'moveForward', interpreter.createNativeFunction((steps) => {
          moveForward(steps);
      }));
      interpreter.setProperty(scope, 'moveBackward', interpreter.createNativeFunction((steps) => {
          moveBackward(steps);
      }));
      interpreter.setProperty(scope, 'moveUp', interpreter.createNativeFunction((steps) => {
          moveUp(steps);
      }));
      interpreter.setProperty(scope, 'moveDown', interpreter.createNativeFunction((steps) => {
          moveDown(steps);
      }));
    });

    function nextStep() {
      if (myInterpreter.step()) {
          setTimeout(nextStep, 150);
      }
  }

  nextStep();
  };
  I did it this way and it worked
Reply all
Reply to author
Forward
0 new messages