Search code examples
javascriptgoto

Using the same code twice in JavaScript


I got a situation like below. somthing happens and the user needs to decide if he want's to proceed or not. If he does not, the code stops executing. If he does it will continue.

However rather than having the same code twice, I'd like that the part (a lot of code) is also executed when the if statment at the top returns a false.

In VB I would have used GOTO but there is no equivalent in Javascript.

if(true){
    var r = confirm("although this and that... do you still want to continue?");
    if (r == false) {
        break;
    } else { 
        a lot of code
    }
}

Any ideas?


Solution

  • Make a function

    function confirmPop(){
       if(true){
        var r = confirm("although this and that... do you still want to continue?");
        if (r == false) {
            break;
        } else { 
            a lot of code
        }
      }
    }
    

    Then just call

    confirmPop();