I'm working on a fullstack project where I need a way to effectuate the following scenario:
Upon a user submit event:
I know there must be a way to work this out, but I haven't been able to figure out how to condense my problem/question into something I can google search my way through.
The following will hopefully help to convey what I'm trying to accomplish:
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;
}
Seems like a job for 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) });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.