最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is it possible to stop execution in javascriptjquery? - Stack Overflow

programmeradmin0浏览0评论

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)

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)

Share Improve this question asked Mar 25, 2009 at 2:30 thirsty93thirsty93 2,6526 gold badges26 silver badges26 bronze badges 3
  • @thirsty: You want queue and dequeue, I think (already suggested and dismissed by you below). You can queue arbitrary methods along with animations and they will execute in order. If this isn't what you are asking then could you clarify your question with an example of what you are trying to do? – Prestaul Commented Mar 25, 2009 at 6:01
  • I have not seen a queue that works for functions. What I mean is if I pass an animation, then a function, then an animation into the queue then while those animations are playing the function should not be executing. Also I do not want to have to specify how long the function will take to execute. – thirsty93 Commented Mar 25, 2009 at 6:58
  • But really what I want is a wrapper that stops all javascript until the function/animation I have passed into it has resolved – thirsty93 Commented Mar 25, 2009 at 6:59
Add a comment  | 

4 Answers 4

Reset to default 10

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

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);
  });
};

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.

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)
       return false;
}

function cancelButtonPress(){
   cancelJob = true;
}
发布评论

评论列表(0)

  1. 暂无评论