Skip to content

Repeat

Repeat控件用于控制循环,可以使用Break控件提前结束循环。

业务属性

属性含义说明输入限定示例值
enable是否启用该控件,未启用时不编译是否编译选择值启用
text与控件关联的文本设计页控件显示常量repeat
expression判断表达式Lua判断表达式Lua脚本var1>var2

判断表达式完全遵循Lua语言的语法,其中不等于逻辑与逻辑或逻辑非分别用~=andornot表示,在判断表达式中变量的表示无需使用#{}占位符,直接书写变量名即可。

与While控件相反,Repeat控件是当表达式值为结束循环,而While控件是当表达式值为进入循环。

同时Repeat控件的执行顺序也与While控件相反,Repeat控件是先执行循环体再进行表达式判断,而While控件是先进行表达式判断再执行循环体。

lua
repeat
--code
until ...
repeat
--code
until ...

使用示例

先执行循环体

设置判断表达式为:true

循环体内的输出控件输出:循环体内

循环体外的输出控件输出:循环体外

如下图:

img_6_4.png

使用curl请求接口:

$ curl http://localhost:6636/api/repeat
$ curl http://localhost:6636/api/repeat

接口返回:

json
{
    "resCode": "0",
    "resMsg": "success",
    "data": "循环体内"
}
{
    "resCode": "0",
    "resMsg": "success",
    "data": "循环体内"
}

由结果可知,第一次进入循环体没有经过判断表达式的判断,即是先执行循环体再进行表达式判断。


循环体执行实例

使用LuaScript控件定义一个数组r1,用于存放输出参数:

lua
local r1={}
local r1={}
img_6.png

将入参body的属性currentValueendValue分别当作循环变量和循环结束值,判断表达式输入:

body.currentValue>=body.endValue

img_6_1.png

使用TableInsert控件往r1数组插入循环变量值:

body.currentValue

img_6_2.png

使用LuaScript控件对循环变量值自增 1

lua
body.currentValue=body.currentValue+1
body.currentValue=body.currentValue+1
img_6_3.png

使用Return控件输出结果r1

json
{"r1":"#{r1}"}
{"r1":"#{r1}"}
img_8.png

使用curl请求接口:

$ curl http://localhost:6636/api/repeat -d "{\"currentValue\":0,\"endValue\":5}"
$ curl http://localhost:6636/api/repeat -d "{\"currentValue\":0,\"endValue\":5}"

接口返回:

json
{
    "resCode": "0",
    "resMsg": "success",
    "data": {
        "r1": [0,1,2,3,4]
    }
}
{
    "resCode": "0",
    "resMsg": "success",
    "data": {
        "r1": [0,1,2,3,4]
    }
}

当循环变量值自增到5时,条件表达式判断为,循环结束。