5

sample code:

var isExecutionOver = false,
    myFunction = function() {
      // does some asynchronous stuff and sets isExecutionOver to true
      // when the asynchronous job is over.
    };

myFunction();

// Kill myFunction if it takes more than 3 seconds
setTimeout(function() {
  if(!isExecutionOver) {
    // How do I kill myFunction?
  }
}, 3*1000);

In the above snippet I am trying to kill (or in other words, stop execution of) myFunction if it is not able to do its job in given time (3 seconds in this case).

PS: Kindly assume that I do not have control over myFunction definition. The only place where I can work is inside setTimeout.

2
  • Depends. What asynchronous stuff is happening? If it's an XHR request, you can .abort() it. To be clear, myFunction will have exited long before the async stuff is done. What you want to cancel is whatever pending async operation that was started by myFunction, not myFunction itself. Commented Jun 5, 2013 at 18:38
  • I don't know where you went, but there's not nearly enough information here to accurately answer your question,. Commented Jun 5, 2013 at 18:59

1 Answer 1

5

You have a very complicated question:

  • You can't actually kill a running function. You can sometimes set a flag that it can notice when it decides to check. It could stop doing what it is doing and return.

    But remember that JavaScript is a single-threaded language. When the code in this function is running, there are few ways for any other code to set that flag.

    Example 1: It seems like I remember something about how some browsers, for example, will let certain events be run from the event queue when an alert box pops up. Those events could set the flag.

    Example 2: Another possibility is that the code in the function is not technically in the function but the function starts an interval timer that does the work. Any other code could set the flag and the code run at intervals could check the flag and stop repeating.

    var abortWork = false;
    function a() {
        while (notDone) {
            .. do some work ...
            if (abortWork) {
                return;
            }
        }
    }
    ... whatever ...
    abortWork = true;
    
  • You can stop a setTimeout() event that is waiting for the time to be over. (There is a similar way to stop a setInterval() repeated event.

    var t = setTimeout(function() {
            ... code goes here ...
        }, 20000);
    ... whatever ...
    clearTimeout(t); // stop the timeout if it hasn't started running the function
    
  • You can stop an ajax request that is waiting for the response. (Note: An XHR object is also returned from pure javascript ajax call.) Note that the request has already gone to the server and this just tells it to ignore any future response to the request.

    var xhr = $.ajax({ type: "POST", ... });
    ... whatever ...
    xhr.abort(); //kill the request
    
Sign up to request clarification or add additional context in comments.

5 Comments

If your first example represents synchronous code, it won't work because the function will block as long as the while is running, so no other code will ever be able to set the abortWork variable.
@CrazyTrain Good point but not 100% applicable. First, use your imagination. Its shown as a loop for clarity but it's random bits of code and could include anything. I'm going to add a bit to make sure your point is reflected, though.
I'd be curious to know what event is allowed to invoke its handler irrespective of blocking code. I've never heard of that before.
Found it. An example was a browser window resize caused by something outside the browser that happened during an alert display and it caused the resize handler to run. stackoverflow.com/questions/2734025/…
Heh, browser quirks are fun!

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.