gpt4 book ai didi

Javascript,运行函数 0.5 秒然后停止(循环)

转载 作者:行者123 更新时间:2023-12-03 03:28:05 24 4
gpt4 key购买 nike

我正在运行安装了 raspberry pi gpio 模块的 Node 服务器。我试图让我的 ESC 启动并运行 0.5 秒,然后我需要它停止 0.5 秒,然后再次循环启动。但是,如果“start”= 0(按钮关闭),我需要循环完全停止并将伺服脉冲宽度设置为 1000(电机停止)

这是我的代码,它还可以。但并没有停止

var start = new blynk.VirtualPin(4);
start.on('write', function t(start) {
if (start == 1) {
setInterval(function() {
setInterval(function() {
motor.servoWrite(1920);
}, 500);
setInterval(function() {
motor.servoWrite(1000);
}, 1000);
}, 500);
} else {
motor.servoWrite(1000);
}
motor.servoWrite(1000);
});

有人可以告诉我哪里出了问题吗?

最佳答案

将间隔存储在变量中

var myInterval = setInterval(function() { [... your code code] },delay);

然后停止它

clearInterval(myInterval);

关于Javascript,运行函数 0.5 秒然后停止(循环),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46217841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com