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)
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)
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毫秒是原始问题和这个答案之间的大概时间...假设我计算正确。
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)
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)
JavaScript setTimeout
是一个非常好的解决方案:
function funcx()
{
// your code here
// break out here if needed
setTimeout(funcx, 3000);
}
funcx();
Run Code Online (Sandbox Code Playgroud)
delay
jQuery中的函数主要用于延迟jQuery动画队列中的动画.
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 循环开始检查当前时间,直到当前时间晚于我们在上一段中指定的时间,一旦发生这种情况,函数就会停止运行,并继续执行到该函数调用之后的时间。
delay()
不停止代码流然后重新运行它.在JavaScript中没有实用的方法.一切都必须完成功能,这些功能采取了setTimeout
其他人提到的回调.
jQuery的目的delay()
是在执行之前使动画队列等待.因此,例如$(element).delay(3000).fadeIn(250);
将使元素在3秒后淡入.
只有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)
归档时间: |
|
查看次数: |
383602 次 |
最近记录: |