Alen
Alen

Reputation: 1291

How to stop function of javascript by pressing button

I have created functions that call other functions like

function abc()
{
    def();
}

function def()
{
    xyz();
}

Lets say def() is called and at any moment I have a button

<button>STOP</button>

if I press it the execution terminates that means if the execution is run again it should run again from start.

I have seen other solutions in SO but they show how to pause a loop. Can anyone help me out?

Upvotes: 1

Views: 10102

Answers (1)

jfren484
jfren484

Reputation: 1000

You could use a variable to determine whether your code runs or terminates:

var abort = false;

function abc()
{
    if (abort) {
        return;
    }

    def();
}

function def()
{
    if (abort) {
        return;
    }

    xyz();
}

<button onclick="abort = true">STOP</button>

The only thing you'd have to add is to reset abort back to false whenever you're triggering the main functionality.

Upvotes: 4

Related Questions