Skip to content Skip to sidebar Skip to footer

Is It Possible To Stop Execution In Javascript/jquery?

Basically I want to be able to wrap any command in a $.holdTillFinished() method that will not allow execution to continue until the specified method/jquery animation is finished e

Solution 1:

I suspect that this may be difficult or even impossible in javascript

Your suspicion is correct. Methods like animations, user input loops and ‘async=true’ XMLHttpRequests must return control to the browser in order to proceed, and the browser can't get back control until every nesting level of function call has returned. That means all your code, including the function that called ‘holdTillFinished()’ would have to unwind: therefore ‘holdTillFinished()’ is impossible.

Other languages have flow-control features that allow you to effectively execute an asynchronous process as one that appears to the caller to be synchronous, and similarly vice-versa. The best-known are threads and continuations. JavaScript does not possess these facilities, so the best you can do is timeouts and callbacks.

(Defining a callback as an inline function to gain access to the enclosing function's variables in a closure does at least take some of the pain out of it; some other languages have to start wrapping every bit of enclosing state in an object's properties to achieve this.)

Solution 2:

You're probably looking for http://docs.jquery.com/Effects/queue

But if you're looking for a more generalized way of delaying things, I've used this snippet before (I didn't write it, so all credit goes to the original author):

//Simple wrapper enabling setTimout within chained functions.
$.fn.wait = function(time, type) {
  time = time || 1000;
  type = type || "fx";
  return this.queue(type, function() {
    var self = this;
    setTimeout(function() {
      $(self).dequeue();
    }, time);
  });
};

Solution 3:

You could use a publish/subscribe architecture to achieve what you want. Without knowing much about what exactly you want to achieve, my guess is this will work for you.

The way this works is that you look at the completion of function as an event, which you can listen to and attach handlers to. Dojo has a pubsub mechanism, though I'm sure there are others.

Solution 4:

It defends upon situation but normally it is necessary to show a processing image to be shown when AJAX is called or any loop is executing in javascript. I have a situation where I am calling AJAX in a loop, for the user to show the processing image is shown. To terminate I done a trick. I have put a button to cancel the job when this function is called I set a global variable say 'cancelJob' to true and in the loop where I am checking this global if this is true the run return false and the script will stop.

var cancelJob = false;
foreach(){
   if(cancelJob == true)
       returnfalse;
}

functioncancelButtonPress(){
   cancelJob = true;
}

Post a Comment for "Is It Possible To Stop Execution In Javascript/jquery?"