Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 executing.

I suspect that this may be difficult or even impossible in javascript, but I'm hoping someone will know.

I don't want to hear about how I can pass a callback into an animation to have it run oncomplete, or anything else like that.

I really want to know if such a thing is possible and how to go about it.

If that isn't going to happen if anyone knows of a nice queue plugin that is capable of queuing both user defined methods and animations, that would be cool too. What I really want is a way to delay execution though.(while still allowing animations to function)

like image 762
thirsty93 Avatar asked Mar 25 '09 02:03

thirsty93


People also ask

How do I stop a jQuery from running?

jQuery | stop() with Examples The stop() method is an inbuilt method in jQuery which is used to stop the currently running animations for the selected element. Syntax: $(selector). stop(stopAll, goToEnd);

What is STOP () in jQuery?

The jQuery stop() method is used to stop an animation or effect before it is finished. The stop() method works for all jQuery effect functions, including sliding, fading and custom animations.

Is it possible to stop script execution on a line?

Is it possible to stop the execution of a python script at any line with a command? The answer is Yes.


Video Answer


2 Answers

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.)

like image 130
bobince Avatar answered Sep 17 '22 17:09

bobince


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);
  });
};
like image 35
FlemishBeeCycle Avatar answered Sep 20 '22 17:09

FlemishBeeCycle