jQuery 如何停止一个函数的运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4851027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop a function from running?
提问by Ryan
I have a function but it keeps on running even if I call a stop()
.
The function name is function slide() { //codes here }
我有一个函数,但即使我调用了它,它也会继续运行stop()
。函数名称是function slide() { //codes here }
slide().stop();
How to stop it from running?
And how to run it again after stopping it?
So I will be having a full control on it.
如何阻止它运行?
以及停止后如何再次运行它?
所以我将完全控制它。
回答by mekwall
.stop
is a method for stopping animations that is running asynchronously (in the background) on the current element. It doesn't interrupt/stop any other code that isn't using the effects library.
.stop
是一种用于停止在当前元素上异步(在后台)运行的动画的方法。它不会中断/停止不使用效果库的任何其他代码。
Instead you should take a look at .queue
which lets you setup custom queues that you can clear or dequeue.
相反,您应该看看.queue
它可以让您设置可以清除或出队的自定义队列。
There already exist very good answers on how to use .queue
so I'll point you to that question instead: Can somebody explain jQuery queue to me?- Look at the answer by gnarf.
关于如何使用已经有很好的答案,.queue
所以我会指向你这个问题:有人可以向我解释 jQuery 队列吗?- 看看 gnarf 的回答。
回答by amurra
If it hits a certain condition in your slide function just use a return;
statement to exit out of the function. Then to start it again just recall the function slide()
. You can't use code from outside of the function to make it exit, it has to be within the function.
如果它在您的幻灯片函数中达到某个条件,只需使用return;
语句退出该函数。然后再次启动它只需调用该功能slide()
。您不能使用函数外部的代码使其退出,它必须在函数内。
回答by bysanchy
Had the same problem and after reading the last answer by amurrathis is what I did...
遇到了同样的问题,在阅读了amurra的最后一个答案后,这就是我所做的......
Declared a global variable by attaching it to the 'window' object. This will act as a flag and decide whether to keep calling the 'slide_it()' function or to stop it (return)
通过将其附加到 'window' 对象来声明一个全局变量。这将作为一个标志并决定是继续调用“slide_it()”函数还是停止它(返回)
window.slide_it = true;
$(element).on({
mouseenter:
function() {
window.slide_it = true;
slide_it();
}
mouseleave:
function() {
window.slide_it = false;
}
});
function slide_it () {
if( !window.slide_it )
return false;
$(element).stop().animate(
{
'left':'-=5px'
},
100,
function() { slide_it() }
);
}
Once you enter the mouse over the $(element) the slide_it() function will iterate itself. And it will stop only if window.slide_it is set to false. So you just set window.slide_it to false when "mouseleave"s
一旦您将鼠标移到 $(element) 上, slide_it() 函数将自行迭代。只有当 window.slide_it 设置为 false 时它才会停止。所以你只需在“鼠标离开”时将 window.slide_it 设置为 false
Hope it helps someone else having the issue!
希望它可以帮助遇到问题的其他人!
回答by Robert Koritnik
If your function is a long running javascript process you should put some kind of outside hooks in it to stop it's execution process and continue when desired. If you have a while loop in it you should be checking against an outside condition to stop its execution. If you don't have a loop in the regular sense of it you will have to find some other way of checking function's running state conditioning.
如果您的函数是一个长时间运行的 javascript 进程,您应该在其中放置某种外部钩子以停止它的执行过程并在需要时继续。如果你有一个 while 循环,你应该检查外部条件以停止它的执行。如果您没有常规意义上的循环,则必须找到其他一些方法来检查函数的运行状态条件。
It's generally not possible to interrupt a running function in Javascript. Not without any additional code that is.
通常不可能在 Javascript 中中断正在运行的函数。并非没有任何额外的代码。
Long running functions will be interrupted by browsers by asking the user whether they want the script to continue execution or rather stop it (browser assumes execution has hanged as in an infinite loop).
浏览器会通过询问用户是否希望脚本继续执行或停止它来中断长时间运行的功能(浏览器假定执行已在无限循环中挂起)。
If you do have a long running function in Javascript you should split fnctionality into several parts and use javascript threadingtechnique that delays execution of short functions one after another by means of window.setTimeout(function, delay);
如果你确实在 Javascript 中有一个长时间运行的函数,你应该将函数分成几个部分,并使用javascript 线程技术,通过一个接一个地延迟短函数的执行window.setTimeout(function, delay);