-
Notifications
You must be signed in to change notification settings - Fork 661
Expand file tree
/
Copy pathwait_block.js
More file actions
59 lines (54 loc) · 1.53 KB
/
wait_block.js
File metadata and controls
59 lines (54 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Example "wait" block that will pause the interpreter for a
* number of seconds. Because wait is a blocking behaviour, such blocks will
* only work in interpreted environments.
*
* See https://neil.fraser.name/software/JS-Interpreter/docs.html
*/
Blockly.defineBlocksWithJsonArray([
{
type: 'wait_seconds',
message0: ' wait %1 seconds',
args0: [
{
type: 'field_number',
name: 'SECONDS',
min: 0,
max: 600,
value: 1,
},
],
previousStatement: null,
nextStatement: null,
colour: '%{BKY_LOOPS_HUE}',
},
]);
/**
* Generator for wait block creates call to new method
* <code>waitForSeconds()</code>.
*/
javascript.javascriptGenerator.forBlock['wait_seconds'] = function (block) {
const seconds = Number(block.getFieldValue('SECONDS'));
const code = 'waitForSeconds(' + seconds + ');\n';
return code;
};
/**
* Register the interpreter asynchronous function
* <code>waitForSeconds()</code>.
*/
function initInterpreterWaitForSeconds(interpreter, globalObject) {
// Ensure function name does not conflict with variable names.
javascript.javascriptGenerator.addReservedWords('waitForSeconds');
const wrapper = interpreter.createAsyncFunction(
function (timeInSeconds, callback) {
// Delay the call to the callback.
setTimeout(callback, timeInSeconds * 1000);
},
);
interpreter.setProperty(globalObject, 'waitForSeconds', wrapper);
}