简体   繁体   English

在执行下一行代码之前,JQuery会等待几秒钟

[英]JQuery wait a number of seconds before the next line of code is executed

I need a simple way or pausing a few seconds before the next line of code is executed. 我需要一个简单的方法或在执行下一行代码之前暂停几秒钟。

So I have: 所以我有:

$('.myClass').show();

//WAIT FOR 5 SECONDS HERE

$('.myClass').hide();

setTimeout : setTimeout

$('.myClass').show();
window.setTimeout(function (){$('.myClass').hide(); }, 5000);

$('.myClass').show().delay(5000).hide(); 。$(” MyClass的)显示()延迟(5000).hide();

Only subsequent events in a queue are delayed; 只有队列中的后续事件才会延迟; for example this will not delay the no-arguments forms of .show() or .hide() which do not use the effects queue. 例如,这不会延迟不使用效果队列的.show()或.hide()的无参数形式。

docs 文档

In order to use delay you have to use duration so it will use the queue: 为了使用delay您必须使用持续时间,因此它将使用队列:

$('.myClass').show().delay(5000).hide(0); 

JSFiddle DEMO JSFiddle DEMO

Thanks @am not i am! 谢谢@am不是我! (again...) (再次...)

This should work: 这应该工作:

$('.myClass').show();
window.setTimeout(  
    function() {  
         $('.myClass').hide();//happens 5 secs later
    },  
    5000
);

See window.setTimeout on MDN 请参阅MDN上的window.setTimeout

You can enclose the hide in a call to setTimeout() . 您可以将hide在对setTimeout()的调用中。

$('.myClass').show();
setTimeout(function() {$('.myClass').hide()}, 5000);
$('.myClass').show();

window.setTimeout(function () {
   $('.myClass').hide();
}, 5000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM