平台介绍 >> js/jsfun/fnAsync.js 异步执行
js/jsfun/fnAsync.js 异步执行
fnAsync.js 异步执行
* 异步执行,无返回值;
函数名称必须是字符串,不能为变量,如: fnAsync_run("fnLog", {"title": "AAAAA"})
异步组执行,有返回值,最后一个变量为函数的执行参数,且个数需要要执行的函数个数一致。
* 同步执行,但同时执行多个异步进程,最终返回json数据;需要确保执行函数存在;执行时间为最长函数执行时间;返回时JSON属性为tag的值
let data = [{"title": "AAAAA"}, {"title": "BBBBB"}]
fnAsync_runGroup("fnLogGroup1", "fnLogGroup2", data)
/**
* 异步
* @param params
* @jssaas url /funAsync.html
* @jssaas method get
* @jssaas timeout 200000
*/
function main_fun_async(params) {
let t1 = fnTime_timestampMilli();
fnAsync_run("fnLog", {"title": "AAAAA"})
let t2 = fnTime_timestampMilli();
return fnRenderTemplate("/html/async.html", {"msg": "已经执行异步,异步10次,时间:" + (t2 - t1)})
}
/**
* 异步日志
* @param params
* @jssaas url /funAsync_log.html
* @jssaas method get
* @jssaas timeout 200000
*/
function main_fun_async_log(params) {
let file = fnJsa_rootPath() + "/aa.txt";
let exist = fnIO_exist(file);
if (exist) {
let content = fnIO_getFileContent(file)
return fnRenderText(content);
}
return fnRenderText("日志还未写入");
}
/**
* 异步
* @param params
* @jssaas url /funAsyncGroup.html
* @jssaas method get
* @jssaas timeout 200000
*/
function main_fun_asyncGroup(params) {
let t1 = fnTime_timestampMilli();
let data = [{"title": "AAAAA"}, {"title": "BBBBB"}]
fnAsync_runGroup("fnLogGroup1", "fnLogGroup2", data)
let t2 = fnTime_timestampMilli();
return fnRenderTemplate("/html/async.html", {"msg": "已经执行异步组,时间:" + (t2 - t1)})
}
function fnLog(params) {
let file = fnJsa_rootPath() + "/aa.txt";
for (let i = 0; i < 10; i++) {
params["time"] = fnTime_timestamp();
let content = fnIO_getFileContent(file)
content = JSON.stringify(params) + "\n
" + content
fnIO_saveFile(file, content, true)
fnTime_sleep(2000);
}
}
function fnLogGroup1(params) {
let file = fnJsa_rootPath() + "/aaa.txt";
params["time"] = fnTime_timestamp();
let content = ""
if(fnIO_exist(file)){
content=fnIO_getFileContent(file)
}
content = JSON.stringify(params) + "\n
" + content
fnIO_saveFile(file, content, true)
fnTime_sleep(2000);
return "fnLogGroup1"
}
function fnLogGroup2(params) {
let file = fnJsa_rootPath() + "/aaa.txt";
params["time"] = fnTime_timestamp();
let content = ""
if(fnIO_exist(file)){
content=fnIO_getFileContent(file)
}
content = JSON.stringify(params) + "\n
" + content
fnIO_saveFile(file, content, true)
fnTime_sleep(5000);
return "fnLogGroup2"
}