-4

I'm currently trying to stop a function that's currently being executed so it can be re-run, like a start/stop button. I've tried to use return; but I'm not sure if I'm using it wrong or not. I've tried throwing errors and that didn't work either.

An example would be like:

function testFunction(action) {
    if (action === "stop") {
        return;
    } else if (action === "start") {
        // do work
    }
}

testFunction("start");

// testFunction("stop"); (stop the function)```
Andrew
  • 1
  • 2
  • Welcome to Stackoverflow! Can you please elaborate your question? Is it that once you pause your function, it must not start until you start it manually? – noob_nerd Jan 31 '21 at 07:26
  • Thank you! I want to be able to start a function or pause it by using a parameter. So if I call the function testFunction("start") it'll start the function. If I do testFunction("stop") it'll stop the function while it's currently running. – Andrew Jan 31 '21 at 07:28
  • It's NOT correct, you should use `clearInterval`! – FZs Jan 31 '21 at 07:28
  • I'm not using setInterval in the final function, it's just used as an example to test if the function is running or not. – Andrew Jan 31 '21 at 07:30
  • You can't stop a synchronous function from running while it's running. If there are asynchronous operations in the function or timers in the function, then you have a chance to be able to stop it. But, we can't help you in any meaningful until you show us the ACTUAL code in the function that you want to stop running. – jfriend00 Jan 31 '21 at 08:16
  • I see that you're probably new to stackoverflow. We don't do very good here with theoretical questions because there are often thousands of possible variations of how one might solve a theoretical question depending upon all the specific circumstances in your real problem and the only complete answer would have to cover all those possibilities. Instead, if you show us your ACTUAL code, we can provide an answer that specifically and unambiguously addresses your exact situation. – jfriend00 Jan 31 '21 at 08:18

1 Answers1

0

I guess you want to stop the execution of the setInterval, for that you could return the setInterval's result from the function and pass it to a clearInterval to avoid it's execution.

For this, you might also have to add a check to see if the function has been started or not But for now, the rewritten script could be something like this

function testFunction(action, process) {
    if (action === "stop") {
        return clearInterval(process);
    } else if (action === "start") {
        return setInterval(() => {
            console.log('hi')
        }, 5000);
    }
}
var process;
process = testFunction("start");
testFunction("stop", process);

Edit
(Since you said that the setInterval case was just an example)
If the code is synchronous then the function will keep executing and you won't be able to run any code after the function call to stop it. If it is asynchronous, it will depend on the code structure, refer to this for an example.

nsrCodes
  • 625
  • 10
  • 25
  • Sorry that I did not make this clear I just re-edited the code. I don't plan on using setInterval in the final function since I only used it just to determine if the function actually stopped or was still running. I just want to know how to stop a function that doesn't use setInterval or setTimeout. – Andrew Jan 31 '21 at 07:33
  • @Andrew if the code is synchronous then the function will keep executing and you won't be able to run any code after the function call to stop it. If it is asynchronous, refer to [this](https://stackoverflow.com/questions/31479197/how-to-stop-asynchronous-function-in-javascript) – nsrCodes Jan 31 '21 at 07:38
  • @Andrew is your code synchronous or asynchronous? – nsrCodes Jan 31 '21 at 07:47
  • @Geek It's asynchronous, but it also calls other functions inside – Andrew Jan 31 '21 at 07:56
  • @Andrew I guess no one can give you further help without seeing the actual code. – nsrCodes Jan 31 '21 at 07:59
  • @Geek I'll try to reference the other post you just sent me as it's similar to what I'm doing. I don't want to provide any actual code from my projects as I'm not authorized to. I'll let you know how it goes though, thank you for your help! – Andrew Jan 31 '21 at 08:00
  • @Andrew no problem, but people will probably flag this post for being vague and not completely informative. Anyways, glad I could help :) – nsrCodes Jan 31 '21 at 08:02
  • 1
    This worked, thank you so much @Geek I've been stuck on this for days, this helps out a lot and gets me closer to what I'm trying to do. – Andrew Feb 02 '21 at 09:37