3

Let's take an example (i am not asking how to cancel/kill a setTimeout at all, i just use it to represent different processing times of different tasks):

<div id="content">
</div>

<p>Which color do you prefer ?</p>    
<button id="long" onClick="long_process();">Blue</button>
<button id="short" onClick="short_process();">Red</button>

<script>
  function long_process() {
    setTimeout(function() {
      $('div#content').text('My favorite color is blue');
    }, 10000);
  }

  function short_proccess() {
    setTimeout(function() {
      $('div#content').text('My favorite color is red');
    }, 1000);
  }

  $('button#long').click();
  $('button#short').click();
</script>

Regarding this example, you probably know already what i am going to ask, how can we stop the long processing action if the user launch a short processing action which is supposed to take over it ?

In this example, if the favorite color of the user is red, but the user click the wrong (blue) button, then click the red button, the blue color willl be the last color displayed because its process is longer.

I know how to manage it with C/C++ using the pthread library, but i have no idea with javascript, and it's very important in some situation to be 100% sure about what your app will display.

Thanks !

6
  • 2
    Shouldn't your question be, how do cancel a setTimeout call? Commented Feb 17, 2013 at 22:08
  • @j08691: Agree. MDN Docs: developer.mozilla.org/en-US/docs/DOM/window.clearTimeout Commented Feb 17, 2013 at 22:09
  • No because in my example setTimeout replace an action (for example calling a database or whatever you can imagine), the thing is, if A and B interract differently with the same target and A is longer than B (differents actions never get exactly the same time to process), if i want B as result but i launched A just before, how to do it ? (this is exactly my example) Commented Feb 17, 2013 at 22:13
  • possible duplicate of How to cancel/kill window.setTimeout before it happens on the client? Commented Feb 17, 2013 at 22:14
  • If you're using a loop you can set some flag or invocation id, and if false/mismatch return;. This does mean extra ops in the loop -> slower performance. There isn't really such a thing as "threads" in JavaScript, except web workers!. Commented Feb 17, 2013 at 23:28

1 Answer 1

2

When creating a timer, setTimeout and setInterval return an ID that you can use to reference that timer:

var long_timer;

function long_process() {
    long_timer = setTimeout(...);
}

When you want to stop that timer, pass that ID into clearTimeout or clearInterval:

if (long_timer) {
    clearTimeout(long_timer);
}
Sign up to request clarification or add additional context in comments.

2 Comments

-1 as I think the gravity of the question does not lie around the setTimeouts. If I am not wrong it is around few asynchronous functions doing some tasks.. and if any of these functions is taking more time than required, then how to stop/kill/abort that function.
@Goje87: You're welcome to post an answer. With the new edits to the original question, I'm not even sure what OP is asking about anymore.

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.