重复一个函数,完成后等待 x 秒
Repeat a function and wait for x seconds after it is complete
我正在开发自己的 node-red 节点,它将使用迷你调制解调器。
所以我有一个调用子进程的函数 executesend。
它看起来像这样:
function executesend(){
exec('echo ' + msg.payload + ' | minimodem --tx ' + options + ' ' + baudmode, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
};
现在我打算实现一个repeat功能,将同一条消息重复发送几次!
if (repeat){
if (typeof(repeatdelay) != 'undefined' && typeof(repeatxtimes) !="undefined"){
for (i = 0; i <= parseInt(repeatxtimestoint); i++){
executesend()
await new Promise(r => setTimeout(r, repeatdelaytoint));
}
}
else{
console.error('Repeat settings not configured!')
node.error('Repeat settings not configured!')
}
}
executesend()
但我注意到所有的重复都是在我发送一条信息后立即发生的。
我相信有一个简单的解决方案,有人可以帮忙吗?
你可以尝试使用setInterval方法,它会每隔x秒调用一个函数。
你可以这样做:
let i = 0;
const interval = setInterval(() => {
// do stuff
i += 1;
if (i === repeatxtimestoint) clearInterval(interval);
}, repeatdelaytoint);
对于您的相同代码。你可以做的是转换执行 send & return promise from it (with async or promise).
function executesend(){
return new Promise((res, rej) => {
// your code
res("when execution gets complete")
})
}
or
async function executesend(){
return "when execution gets complete"
};
并在您的另一个 for 循环代码中。
// 编辑新起点
如果内部使用了 await,请确保使用 async 声明函数。
错误来自语法错误。
因为没有异步使用 await 是不允许的。
async function yourFunctionWithForLoop(){
for (i = 0; i <= parseInt(repeatxtimestoint); i++){
await executesend()
await new Promise(r => setTimeout(r, repeatdelaytoint));
}
}
// 编辑新结尾
当前在您的代码中,settimeout(对于循环一)在 exec 执行之前被调用并在给定的任何时间后得到解决。
希望对你有帮助
我正在开发自己的 node-red 节点,它将使用迷你调制解调器。 所以我有一个调用子进程的函数 executesend。 它看起来像这样:
function executesend(){
exec('echo ' + msg.payload + ' | minimodem --tx ' + options + ' ' + baudmode, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
};
现在我打算实现一个repeat功能,将同一条消息重复发送几次!
if (repeat){
if (typeof(repeatdelay) != 'undefined' && typeof(repeatxtimes) !="undefined"){
for (i = 0; i <= parseInt(repeatxtimestoint); i++){
executesend()
await new Promise(r => setTimeout(r, repeatdelaytoint));
}
}
else{
console.error('Repeat settings not configured!')
node.error('Repeat settings not configured!')
}
}
executesend()
但我注意到所有的重复都是在我发送一条信息后立即发生的。 我相信有一个简单的解决方案,有人可以帮忙吗?
你可以尝试使用setInterval方法,它会每隔x秒调用一个函数。
你可以这样做:
let i = 0;
const interval = setInterval(() => {
// do stuff
i += 1;
if (i === repeatxtimestoint) clearInterval(interval);
}, repeatdelaytoint);
对于您的相同代码。你可以做的是转换执行 send & return promise from it (with async or promise).
function executesend(){
return new Promise((res, rej) => {
// your code
res("when execution gets complete")
})
}
or
async function executesend(){
return "when execution gets complete"
};
并在您的另一个 for 循环代码中。 // 编辑新起点 如果内部使用了 await,请确保使用 async 声明函数。 错误来自语法错误。 因为没有异步使用 await 是不允许的。
async function yourFunctionWithForLoop(){
for (i = 0; i <= parseInt(repeatxtimestoint); i++){
await executesend()
await new Promise(r => setTimeout(r, repeatdelaytoint));
}
}
// 编辑新结尾
当前在您的代码中,settimeout(对于循环一)在 exec 执行之前被调用并在给定的任何时间后得到解决。
希望对你有帮助