0

I have a webpage with 3 buttons and a "result" section:

<button id="b1" onclick="f1()">1st button</button>
<button id="b2" onclick="f2()">2nd button</button>
<button id="b3" onclick="f3()">3rd button</button>
<p id="result">"Here will be the result!"</p>

Here are the snippets for the JavaScript functions:

var to;

function f1() {
    // Kill other running instances of all functions
    to = setTimeout(() => {
        document.getElementById("result").innerHTML = "1st function";
    }, 10000 * Math.random());
}

f2 and f3 are basically the same, but prints different outputs after the less-than-10-second pause.

However, if the user clicks b1 and then immediately click b2, there is a possibility that the innerHTML within f1 gets run after that in f2, and the output would be 1st function, instead of 2nd function.

What I want is to implement, at the start of each function, a statement that kills/ignores/bypasses all functions that are currently running so that the final output would always be the one associated with the last button the user pressed. How can it be best implemented?

2 Answers 2

1

You can set a global variable that all the functions can see and use it to store the return value from setTimeout(). You are already doing this. All you need to do now is call clearTimeout() at the head of each function to prevent the previous call from executing.

The effect will be that the last button to be pressed will be the only one that wins.

var to;

function f1() {
  clearTimeout(to)
  // Kill other running instances of all functions
  to = setTimeout(() => {
    document.getElementById("result").innerHTML = "1st function";

  }, 3000 * Math.random());
}

function f2() {
  clearTimeout(to)
  // Kill other running instances of all functions
  to = setTimeout(() => {
    document.getElementById("result").innerHTML = "2nd function";

  }, 3000 * Math.random());
}

function f3() {
  clearTimeout(to)
  // Kill other running instances of all functions
  to = setTimeout(() => {
    document.getElementById("result").innerHTML = "3rd function";

  }, 3000 * Math.random());
}
<button id="b1" onclick="f1()">1st button</button>
<button id="b2" onclick="f2()">2nd button</button>
<button id="b3" onclick="f3()">3rd button</button>
<p id="result">"Here will be the result!"</p>

Sign up to request clarification or add additional context in comments.

1 Comment

Working elegantly!
1

Use a boolean to stop the function from running, if it has been pressed once before.

var pause = false;
var result = document.getElementById("result");

function genericF(text) {
    if(pause) return;
    pause = true;
    // Kill other running instances of all functions
    setTimeout(() => {
        result.innerHTML = text;
        pause = false;
    }, 10000 * Math.random());
}
<button id="b1" onclick="genericF('1st button')">1st button</button>
<button id="b2" onclick="genericF('2nd button')">2nd button</button>
<button id="b3" onclick="genericF('3rd button')">3rd button</button>
<p id="result">"Here will be the result!"</p>

2 Comments

The final output will always be the first button pressed, not the last.
I believe that this blocks further inputs until the first function finishes. What I would like to see is the early termination of the first function and immediate start of the second function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.