jQuery:等待/延迟1秒而不执行代码

Bri*_*ham 117 javascript jquery delay wait seconds

我无法.delay在jQuery中使用该方法:

$.delay(3000); // not working
$(queue).delay(3000); // not working
Run Code Online (Sandbox Code Playgroud)

我正在使用while循环来等待一个不受控制的更改值大于或等于另一个并且我找不到任何方法来执行X秒的执行.

Mat*_*ich 198

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

setTimeout(function (){

  // Something you want delayed.

}, 5000); // How long do you want the delay to be (in milliseconds)? 
Run Code Online (Sandbox Code Playgroud)

  • 仅仅是一个FYI,这不会破坏执行流程,所以如果你需要"停止一切"几秒钟,你将需要像Niessner所发布的那样. (6认同)

Jus*_*ner 166

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

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

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

check();
Run Code Online (Sandbox Code Playgroud)

  • 我想,setTimeout就够了。 (2认同)
  • 你的炸弹兄弟,这正是我需要修复我的代码. (2认同)

the*_*tto 13

ES6 setTimeout

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

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

  • 使用从原始问题到这个答案的大概时间,这真的很不错 (2认同)

Ant*_*met 11

如果你正在使用 ES6 特性并且你在一个异步函数中,你可以使用这个函数有效地暂停代码执行一段时间:

const delay = millis => new Promise((resolve, reject) => {
  setTimeout(_ => resolve(), millis)
});
Run Code Online (Sandbox Code Playgroud)

这是你如何使用它:

await delay(5000);
Run Code Online (Sandbox Code Playgroud)

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

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

  await delay(5000);

  // some more code, executed 5 seconds after the first code block finishes
}
Run Code Online (Sandbox Code Playgroud)

  • 等待新的 Promise(resolve => setTimeout(resolve, 1000)); (2认同)

Jul*_* D. 9

jQuery的delay函数用于效果和效果队列,请参阅delay文档和其中的示例:

$('#foo').slideUp(300).delay(800).fadeIn(400);
Run Code Online (Sandbox Code Playgroud)

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

(function() {
    var observerInterval = setInterval(function() {
        if (/* check for changes here */) {
           clearInterval(observerInterval);
           // do something here
        }
    }, 1000);
})();
Run Code Online (Sandbox Code Playgroud)


Jay*_*ten 8

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

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

funcx();
Run Code Online (Sandbox Code Playgroud)

delayjQuery中的函数主要用于延迟jQuery动画队列中的动画.


don*_*key 6

    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');
Run Code Online (Sandbox Code Playgroud)

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

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

  • 这种方式对CPU的占用非常大,这样的睡眠可能会导致网页冻结,用户不会喜欢它。不要使用它。 (2认同)

Nat*_*nes 5

delay()不停止代码流然后重新运行它.在JavaScript中没有实用的方法.一切都必须完成功能,这些功能采取了setTimeout其他人提到的回调.

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


Fab*_*uda 5

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


Far*_*mar 5

只有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>
Run Code Online (Sandbox Code Playgroud)

  • 在 JavaScript 中循环等待是一个坏主意!当循环运行时,所有 JavaScript 功能都将**停止**。这将会产生意想不到的副作用。最好使用像 setTimeout() 这样的非阻塞函数。 (2认同)