155

我无法让该.delay方法在 jQuery 中工作:

$.delay(3000); // not working
$(queue).delay(3000); // not working

我正在使用一个while循环来等待一个不受控制的变化值大于或等于另一个值,并且我找不到任何方法来拖拉执行X秒。

4

10 回答 10

257

您也可以通过这种方式延迟一些操作:

setTimeout(function (){
  
  // Something you want delayed.
            
}, 5000); // How long you want the delay to be, measured in milliseconds.
于 2012-07-21T01:31:01.490 回答
192

$.delay 用于延迟队列中的动画,而不是停止执行。

您需要递归调用一个每秒执行检查的方法,而不是使用 while 循环setTimeout

var check = function(){
    if(condition){
        // run when condition is met
    }
    else {
        setTimeout(check, 1000); // check again in a second
    }
}

check();
于 2012-01-17T14:32:02.367 回答
36

ES6 设置超时

setTimeout(() => {
  console.log("we waited 204586560000 ms to run this code, oh boy wowwoowee!");
}, 204586560000);

编辑: 204586560000 ms 是原始问题和这个答案之间的大致时间......假设我计算正确。

于 2018-07-12T18:05:28.363 回答
16

如果您正在使用 ES6 功能并且处于异步函数中,则可以使用此函数有效地暂停代码执行一段时间:

const delay = millis => new Promise((resolve, reject) => {
  setTimeout(_ => resolve(), millis)
});

这是你如何使用它:

await delay(5000);

它会在请求的毫秒数内停止,但前提是您处于 async function中。下面的例子:

const myFunction = async function() {
  // first code block ...

  await delay(5000);

  // some more code, executed 5 seconds after the first code block finishes
}
于 2020-04-08T10:30:40.543 回答
10

jQuery 的delay函数旨在与效果和效果队列一起使用,请参阅delay文档和其中的示例:

$('#foo').slideUp(300).delay(800).fadeIn(400);

如果你想观察一个变量的变化,你可以做类似的事情

(function() {
    var observerInterval = setInterval(function() {
        if (/* check for changes here */) {
           clearInterval(observerInterval);
           // do something here
        }
    }, 1000);
})();
于 2012-01-17T14:43:03.837 回答
9

JavaScriptsetTimeout是一个非常好的解决方案:

function funcx()
   {
   // your code here
   // break out here if needed
   setTimeout(funcx, 3000);
   }

funcx();

jQuery 中的delay函数主要用于延迟 jQuery 动画队列中的动画。

于 2012-01-17T14:36:47.250 回答
7

delay()不会停止代码流然后重新运行它。在 JavaScript 中没有实用的方法可以做到这一点。一切都必须使用接受回调的函数来完成,例如setTimeout其他人提到的。

jQuery 的目的delay()是让动画队列在执行前等待。因此,例如$(element).delay(3000).fadeIn(250);将使元素在 3 秒后淡入。

于 2012-01-17T14:37:20.100 回答
5
    function sleep(num) {
        let now = new Date();
        let stop = now.getTime() + num;
        while(true) {
            now = new Date();
            if(now.getTime() > stop) return;
        }
    }

    sleep(1000);   // 1 second 
    alert('here');

它以毫秒为单位获取当前时间,添加num(这是未来时间),您num在当前时间中添加了许多毫秒。

一个无限的while循环开始检查当前时间,直到当前时间晚于我们在上一段中指定的时间,一旦发生,函数将停止运行,并继续执行该函数调用之后的任何时间。

于 2021-02-24T10:52:10.040 回答
4

Javascript 是一种异步编程语言,因此您不能暂时停止执行;您可以[伪]停止执行的唯一方法是使用 setTimeout() ,这不是延迟,而是“延迟函数回调”。

于 2012-01-17T14:56:56.657 回答
4

只有 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>
于 2016-03-10T10:02:23.627 回答