I have an HTML page with some data that needs to be saved or discarded before certain other things can be done. I have several functions that will need to pause, wait for the user to make a decision, and then, based on that decision, continue on one of two paths. The kicker is that each function is different. For example, function A will execute 3 lines of code, ask for input, and then execute another 2-4 lines depending on the answer. Function B will execute 2 different lines of code, ask for input, and then execute another set of code. I know that I can use prompt() to get a browser dialog box but I don't want that. Instead, I want the user to click a button in a modal. How would I do that so that the code is reusable.
I know I can break each of these up into 2 separate functions (for a total of 4 below) but I would prefer not to have to do that.
// Note: I know this code doesn't make a lot of sense and isn't runnable. It is to demonstrate the
// structure of what I'm trying to do.
function doSomething() {
let a = now();
console.log("it is" + a);
// TODO: show a modal asking if save or discard changes
if (discard) {
$("myContainer").replaceWith("<div></div>");
} else {
saveChanges();
}
function doSomethingElse() {
let b = 12 * 12;
console.log("12 x 12 is" + b);
let c = 1 + 1;
console.log("1 + 1 is" + c);
// TODO: show a modal asking if save or discard changes
// Wait for user input
if (discard) {
$("myContainer").replaceWith("<div></div>");
} else {
alert("Saving Changes Now!");
saveChanges();
}
$("#button1").on("click", doSomething);
$("#button2").on("click", doSomethingElse");