mush js语言初步实现分间隔执行方法
var p_isStop = false;var p_isPause = false;
//默认由多个|号分割的命令,按second的间隔执行秒,
//start为执行的方法位置:例如:cmdstr为func1()|func2(),当start为0即执行func1(),为1即执行func2()
function doAfterSpecial3(cmdstr,second,start){
if(start == null){
start = 0;
}
p_isStop = false;
var cmds = cmdstr.split("|");
if(cmds.length <= start){
world.Note("doAfterSpecial3:执行完成");
return;
}
if(p_isStop){
world.Note("doAfterSpecial3:停止");
return;
}
var cmd = cmds;
start++;
var fcmd = "world.Note('"+cmd+"');doAfterSpecial3('"+cmdstr+"',"+second+","+start+")";
world.DoAfterSpecial(second,"canExcute(\""+fcmd+"\")",12);
}
function canExcute(cmd){
if(p_isPause){
world.DoAfterSpecial(5,"canExcute('"+cmd+"')",12);
}else{
world.Note("canExcute:"+cmd);
world.DoAfterSpecial(0.1,cmd,12);
}
}
如需优化改善的地方,请大家指出,谢谢!
北大侠客行MUD,中国最好的MUD 改良了一下:
var p_isStop = false;
var p_isPause = false;
//默认由多个|号分割的命令,按second的间隔执行秒,
//start为执行的方法位置:例如:cmdstr为func1()|func2(),当start为0即执行func1(),为1即执行func2()
//type为执行类型10为命令,12为方法
function doAfterSpecial3(cmdstr,second,type,start){
if(start == null){
start = 0;
}
if(type == null){
type = 12;
}
if(second < 1 || second > 100){
world.Note("间隔超出范围");
}
p_isStop = false;
var cmds = cmdstr.split("|");
if(cmds.length <= start){
world.Note("doAfterSpecial3:执行完成");
return;
}
if(p_isStop){
world.Note("doAfterSpecial3:停止");
return;
}
var cmd = cmds;
start++;
var fcmd = "world.DoAfterSpecial(0.1,\\\""+cmd+"\\\","+type+");doAfterSpecial3(\\\""+cmdstr+"\\\","+second+","+type+","+start+")";
world.DoAfterSpecial(second,"canExcute(\""+fcmd+"\")",12);
}
function canExcute(cmd){
if(p_isPause){
world.DoAfterSpecial(5,"canExcute('"+cmd+"')",12);
}else{
world.Note("canExcute:"+cmd);
world.DoAfterSpecial(0.1,cmd,12);
}
}
调用方法:doAfterSpecial3("world.Note('n')|world.Note('n')|world.Note('west')","2","12")
RE: 改良v0.2
改良了一下,增加完成执行完命令后的回调方法与停止后的回调方法另外修改原有不能立即停止的bug
var p_isStop = false;
var p_isPause = false;
//记录暂停房间
var p_pauseRoomId = "";
//默认由多个|号分割的命令,按second的间隔执行秒,
//start为执行的方法位置:例如:cmdstr为func1()|func2(),当start为0即执行func1(),为1即执行func2()
//type为执行类型10为命令,12为方法
//callback为执行完命令后的回调方法
//stopcallback为停止后的回调方法
function doAfterSpecial3(cmdstr,second,type,start,callback,stopcallback){
if(start == null){
start = 0;
}
if(type == null){
type = 12;
}
if(second < 0.5 || second > 100){
world.Note("间隔超出范围");
}
if(callback == null){
callback = "";
}
if(stopcallback == null){
stopcallback = "";
}
var cmds = cmdstr.split("|");
if(cmds.length <= start){
world.Note("doAfterSpecial3:执行完成");
if(callback != ""){
eval(callback+"()");
}
return;
}
if(p_isStop){
world.Note("doAfterSpecial3:停止");
if(stopcallback != ""){
//p_doAfterSpecial3_stopcallback();
eval(stopcallback+"()");
}
return;
}
var cmd = cmds;
start++;
var fcmd = "world.DoAfterSpecial(0.1,\\\""+cmd+"\\\","+type+");";
if(type == 10){
fcmd = "world.Execute(\\\""+cmd+"\\\");";
}else if(type = 12){
fcmd = "eval(\\\""+cmd+"\\\");";
}
var fcmd2 = "doAfterSpecial3(\\\""+cmdstr+"\\\","+second+","+type+","+start+",\\\""+callback+"\\\",\\\""+stopcallback+"\\\")";
if(callback == ""){
if(stopcallback == ""){
fcmd2 = "doAfterSpecial3(\\\""+cmdstr+"\\\","+second+","+type+","+start+")";
}
}else{
if(stopcallback == ""){
fcmd2 = "doAfterSpecial3(\\\""+cmdstr+"\\\","+second+","+type+","+start+",\\\""+callback+"\\\")";
}
}
fcmd += fcmd2;
world.DoAfterSpecial(second,"canExcute(\""+fcmd+"\")",12);
}
function canExcute(cmd){
if(p_isPause){
cmd = cmd.replace(/'/g,"\\\'");
world.DoAfterSpecial(5,"canExcute('"+cmd+"')",12);
}else{
//world.Note("canExcute:"+cmd);
//world.DoAfterSpecial(0.1,cmd,12);
eval(cmd);
}
}
function test1(){
world.Note("callback");
}
function test2(){
world.Note("stopcallback");
}
调用方法:doAfterSpecial3("world.Note('n')|world.Note('n')|world.Note('west')","2","12",0,"test1","test2")
页:
[1]