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.

Goje87
  • 2,839
  • 7
  • 28
  • 48
  • 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. –  Jun 05 '13 at 18:38
  • I don't know where you went, but there's not nearly enough information here to accurately answer your question,. –  Jun 05 '13 at 18:59

1 Answers1

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
    
Lee Meador
  • 12,829
  • 2
  • 36
  • 42
  • 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. –  Jun 05 '13 at 18:55
  • @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. – Lee Meador Jun 05 '13 at 20:13
  • 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. –  Jun 05 '13 at 21:10
  • 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. http://stackoverflow.com/questions/2734025/is-javascript-guaranteed-to-be-single-threaded – Lee Meador Jun 05 '13 at 21:22
  • 2
    Heh, browser quirks are fun! –  Jun 05 '13 at 21:26