Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill a javascript function?

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.

like image 974
Goje87 Avatar asked Jun 05 '13 18:06

Goje87


People also ask

How do you end a function in JavaScript?

Using return to exit a function in javascript Using return is the easiest way to exit a function. You can use return by itself or even return a value.

How do I stop a function from running?

Running functions can only be terminated from within the function and that is done with either a return statement or by throwing an exception. return can be called conditionally so that the function doesn't always exit at the same point.

Which of the following terms stops the execution of a function?

Which of the following terms stops the execution of a function? Explanation: The return stops the execution of the function when it is encountered within the function.


1 Answers

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
    
like image 114
Lee Meador Avatar answered Sep 28 '22 08:09

Lee Meador