[英]How can I construct this server backend route?
我正在做一个全栈项目,我需要一种方法来实现以下场景:
在用户提交事件时:
我知道一定有办法解决这个问题,但我一直无法弄清楚如何将我的问题/问题浓缩成我可以通过谷歌搜索的东西。
以下内容有望帮助传达我要完成的工作:
let run_function = false;
app.post('/start', (req, res) => {
run_function = true;
while (run_function)
the_function(req.body);
}
app.get('/stop', (req, res) => {
run_function = false;
}
似乎是setInterval() / clearInterval()的工作
let handle
// Default 200ms interval
const startLoop = (body, interval = 200) => {
clearInterval(handle) // stop any previous loops
handle = setInterval(the_function, interval, body)
}
app.post("/start", (req, res) => {
startLoop(req.body)
res.send("Started")
})
app.post("/stop", (req, res) => {
clearInterval(handle)
res.send("Stopped")
})
let LoopInterval = null; app.post('/start', (req, res) => { LoopInterval = setInterval(() => { the_function(req.body); }, 0); }); app.get('/stop', (req, res) => { clearInterval(LoopInterval) });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.