I can't get the .delay
method working in jQuery:
我无法让.delay方法在jQuery中工作:
$.delay(3000); // not working
$(queue).delay(3000); // not working
I'm using a while loop to wait until an uncontrolled changing value is greater than or equal to another and I can't find any way to hault execution for X seconds.
我正在使用一个while循环来等待一个不受控制的更改值大于或等于另一个,我找不到任何方法来在X秒内执行。
7 个解决方案
#1
138
$.delay is used to delay animations in a queue, not halt execution.
$.delay用于延迟队列中的动画,而不是停止执行。
Instead of using a while loop, you need to recursively call a method that performs the check every second using setTimeout
:
不使用while循环,您需要递归调用使用setTimeout每秒执行检查的方法:
var check = function(){
if(condition){
// run when condition is met
}
else {
setTimeout(check, 1000); // check again in a second
}
}
check();
#2
168
You can also just delay some operation this way:
你也可以这样延迟一些操作:
setTimeout(function (){
// Something you want delayed.
}, 5000); // How long do you want the delay to be (in milliseconds)?
#3
7
JavaScript setTimeout
is a very good solution:
JavaScript setTimeout是一个非常好的解决方案:
function funcx()
{
// your code here
// break out here if needed
setTimeout(funcx, 3000);
}
funcx();
The delay
function in jQuery is mostly used for delaying animations in a jQuery animation queue.
jQuery中的delay函数主要用于延迟jQuery动画队列中的动画。
#4
7
jQuery's delay
function is meant to be used with effects and effect queues, see the delay
docs and the example therein:
jQuery的延迟函数用于与效果和效果队列一起使用,请参阅延迟文档和其中的示例:
$('#foo').slideUp(300).delay(800).fadeIn(400);
If you want to observe a variable for changes, you could do something like
如果您想要观察一个变量的变化,您可以做如下的事情
(function() {
var observerInterval = setInterval(function() {
if (/* check for changes here */) {
clearInterval(observerInterval);
// do something here
}
}, 1000);
})();
#5
5
delay()
doesn't halt the flow of code then re-run it. There's no practical way to do that in JavaScript. Everything has to be done with functions which take callbacks such as setTimeout
which others have mentioned.
delay()不会停止代码流,然后重新运行它。在JavaScript中没有实际的方法。所有的事情都必须使用函数来完成,这些函数接受回调,比如其他人提到的setTimeout。
The purpose of jQuery's delay()
is to make an animation queue wait before executing. So for example $(element).delay(3000).fadeIn(250);
will make the element fade in after 3 seconds.
jQuery delay()的目的是使动画队列在执行之前等待。例如$(元素).delay(3000).fadeIn(250);将使元素在3秒后消失。
#6
4
Only javascript It will work without jQuery
只有javascript,它的工作没有jQuery。
<!DOCTYPE html>
<html>
<head>
<script>
function sleep(miliseconds) {
var currentTime = new Date().getTime();
while (currentTime + miliseconds >= new Date().getTime()) {
}
}
function hello() {
sleep(5000);
alert('Hello');
}
function hi() {
sleep(10000);
alert('Hi');
}
</script>
</head>
<body>
<a href="#" onclick="hello();">Say me hello after 5 seconds </a>
<br>
<a href="#" onclick="hi();">Say me hi after 10 seconds </a>
</body>
</html>
#7
3
Javascript is an asynchronous programming language so you can't stop the execution for a of time; the only way you can [pseudo]stop an execution is using setTimeout() that is not a delay but a "delayed function callback".
Javascript是一种异步编程语言,因此您不能停止执行一段时间;惟一可以[伪]停止执行的方法是使用setTimeout(),它不是延迟,而是“延迟函数回调”。
#1
138
$.delay is used to delay animations in a queue, not halt execution.
$.delay用于延迟队列中的动画,而不是停止执行。
Instead of using a while loop, you need to recursively call a method that performs the check every second using setTimeout
:
不使用while循环,您需要递归调用使用setTimeout每秒执行检查的方法:
var check = function(){
if(condition){
// run when condition is met
}
else {
setTimeout(check, 1000); // check again in a second
}
}
check();
#2
168
You can also just delay some operation this way:
你也可以这样延迟一些操作:
setTimeout(function (){
// Something you want delayed.
}, 5000); // How long do you want the delay to be (in milliseconds)?
#3
7
JavaScript setTimeout
is a very good solution:
JavaScript setTimeout是一个非常好的解决方案:
function funcx()
{
// your code here
// break out here if needed
setTimeout(funcx, 3000);
}
funcx();
The delay
function in jQuery is mostly used for delaying animations in a jQuery animation queue.
jQuery中的delay函数主要用于延迟jQuery动画队列中的动画。
#4
7
jQuery's delay
function is meant to be used with effects and effect queues, see the delay
docs and the example therein:
jQuery的延迟函数用于与效果和效果队列一起使用,请参阅延迟文档和其中的示例:
$('#foo').slideUp(300).delay(800).fadeIn(400);
If you want to observe a variable for changes, you could do something like
如果您想要观察一个变量的变化,您可以做如下的事情
(function() {
var observerInterval = setInterval(function() {
if (/* check for changes here */) {
clearInterval(observerInterval);
// do something here
}
}, 1000);
})();
#5
5
delay()
doesn't halt the flow of code then re-run it. There's no practical way to do that in JavaScript. Everything has to be done with functions which take callbacks such as setTimeout
which others have mentioned.
delay()不会停止代码流,然后重新运行它。在JavaScript中没有实际的方法。所有的事情都必须使用函数来完成,这些函数接受回调,比如其他人提到的setTimeout。
The purpose of jQuery's delay()
is to make an animation queue wait before executing. So for example $(element).delay(3000).fadeIn(250);
will make the element fade in after 3 seconds.
jQuery delay()的目的是使动画队列在执行之前等待。例如$(元素).delay(3000).fadeIn(250);将使元素在3秒后消失。
#6
4
Only javascript It will work without jQuery
只有javascript,它的工作没有jQuery。
<!DOCTYPE html>
<html>
<head>
<script>
function sleep(miliseconds) {
var currentTime = new Date().getTime();
while (currentTime + miliseconds >= new Date().getTime()) {
}
}
function hello() {
sleep(5000);
alert('Hello');
}
function hi() {
sleep(10000);
alert('Hi');
}
</script>
</head>
<body>
<a href="#" onclick="hello();">Say me hello after 5 seconds </a>
<br>
<a href="#" onclick="hi();">Say me hi after 10 seconds </a>
</body>
</html>
#7
3
Javascript is an asynchronous programming language so you can't stop the execution for a of time; the only way you can [pseudo]stop an execution is using setTimeout() that is not a delay but a "delayed function callback".
Javascript是一种异步编程语言,因此您不能停止执行一段时间;惟一可以[伪]停止执行的方法是使用setTimeout(),它不是延迟,而是“延迟函数回调”。