本文介绍了是否可以在javascript / jquery中停止执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我希望能够在$ .holdTillFinished()方法中包装任何命令,该方法不允许执行继续,直到指定的方法/ 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.

我怀疑这在javascript中可能很难甚至不可能,但我希望有人会知道。

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)

推荐答案

您的怀疑是正确的。像动画,用户输入循环和'async = true'这样的方法XMLHttpRequests 必须将控制权返回给浏览器才能继续,并且浏览器无法恢复控制,直到函数调用的每个嵌套级别都返回。这意味着你的所有代码,包括调用'holdTillFinished()'的函数都必须放松:因此'holdTillFinished()'是不可能的。

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.

其他语言有流量 - 控制功能,允许您有效地执行异步进程,使其成为调用方看起来同步的异步进程,反之亦然。最着名的是和。 JavaScript不具备这些功能,因此您可以做的最好的是超时和回调。

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

这篇关于是否可以在javascript / jquery中停止执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 08:16