Async function

This commit is contained in:
ckcz123 2018-12-05 16:48:33 +08:00
parent a6a7671543
commit 5401714b3f
3 changed files with 18 additions and 10 deletions

View File

@ -1490,15 +1490,16 @@ return code;
function_s
: '自定义JS脚本' BGNL? Newline RawEvalString Newline BEND Newline
: '自定义JS脚本' '不自动执行下一个事件' Bool BGNL? Newline RawEvalString Newline BEND Newline
/* function_s
tooltip : 可双击多行编辑请勿使用异步代码。常见API参见文档附录。
helpUrl : https://h5mota.com/games/template/docs/#/event?id=function%EF%BC%9A%E8%87%AA%E5%AE%9A%E4%B9%89js%E8%84%9A%E6%9C%AC
default : ["alert(core.getStatus(\"atk\"));"]
default : [false,"alert(core.getStatus(\"atk\"));"]
colour : this.dataColor
var code = '{"type": "function", "function": "function(){\\n'+JSON.stringify(RawEvalString_0).slice(1,-1).split('\\\\n').join('\\n')+'\\n}"},\n';
Bool_0 = Bool_0?', "async": true':'';
var code = '{"type": "function"'+Bool_0+', "function": "function(){\\n'+JSON.stringify(RawEvalString_0).slice(1,-1).split('\\\\n').join('\\n')+'\\n}"},\n';
return code;
*/;
@ -2225,7 +2226,7 @@ ActionParser.prototype.parseAction = function() {
var func = data["function"];
func=func.split('{').slice(1).join('{').split('}').slice(0,-1).join('}').trim().split('\n').join('\\n');
this.next = MotaActionBlocks['function_s'].xmlText([
func,this.next]);
data.async||false,func,this.next]);
break;
case "update":
this.next = MotaActionBlocks['update_s'].xmlText([

View File

@ -1610,6 +1610,8 @@ core.insertAction([
// 请勿直接调用 core.changeFloor(toFloor, ...),这个代码是异步的,会导致事件处理和录像出问题!
```
!> 从V2.5.3开始,提供了一个"不自动执行下一个事件"的选项(`"async": true`)。如果设置了此项,那么在该部分代码执行完毕后,不会立刻执行下一个事件。你需要在脚本中手动调用`core.events.doAction()`来执行下一个事件。可以通过此项来实现一些异步的代码,即在异步函数的回调中再执行下一个事件。使用此选项请谨慎,最好向开发者寻求咨询。
## 同一个点的多事件处理
我们可以发现,就目前而且,每个点的事件是和该点进行绑定,并以该点坐标作为唯一索引来查询。

View File

@ -1053,14 +1053,19 @@ events.prototype.doAction = function() {
case "function":
{
var func = data["function"];
if (core.isset(func)) {
if ((typeof func == "string") && func.indexOf("function")==0) {
eval('('+func+')()');
try {
if (core.isset(func)) {
if ((typeof func == "string") && func.indexOf("function")==0) {
eval('('+func+')()');
}
else if (func instanceof Function)
func();
}
else if (func instanceof Function)
func();
} catch (e) {
console.log(e);
}
this.doAction();
if (!data.async)
this.doAction();
break;
}
case "update":