如果我理解正确!
您可以构建一个新类来处理执行go(dir, dist)函数,并覆盖go函数以在执行器中创建新的go。
function GoExecutor(){
var executeArray = [];
var isRunning = false;
var run = function(){
if(isRunning)
return;
isRunning = true;
runner();
}
var runner = function(){
if(executeArray.length == 0){
isRunning = false;
return;
}
var currentExec = executeArray.shift(0);
setTimeout(function(){
originalGoFunction(currentExec.dir, currentExec.dist);
runner();
}, currentExec.delay);
}
this.push = function(dir, dist){
executeArray.push([dir,dist]);
run();
}
}
var goExec = new GoExecutor();
var originalGoFunction = go;
var go = function (dir, dist, delay){
goExec.push({"dir":dir, "dist":dist, "delay":delay});
}
编辑1:
现在您需要调用callWithDelay并传入您的函数和参数,执行程序将通过将参数应用于指定的函数来处理此调用。
function GoExecutor(){
var executeArray = [];
var isRunning = false;
var run = function(){
if(isRunning)
return;
isRunning = true;
runner();
}
var runner = function(){
if(executeArray.length == 0){
isRunning = false;
return;
}
var currentExec = executeArray.shift(0);
setTimeout(function(){
currentExec.funcNam.apply(currentExec.funcNam, currentExec.arrayParams);
runner();
}, currentExec.delay);
}
this.push = function(dir, dist){
executeArray.push([dir,dist]);
run();
}
}
var goExec = new GoExecutor();
var callWithDelay = function (func, arrayParams, delay){
goExec.push({"func": func, "arrayParams":arrayParams, "delay":delay});
}
async/await
的环境,你可以编写看似同步的代码,但最终必须是异步的。 - zzzzBov