简体   繁体   中英

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 :

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

$('.myClass').show().delay(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.

docs

In order to use delay you have to use duration so it will use the queue:

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

JSFiddle DEMO

Thanks @am not i am! (again...)

This should work:

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

See window.setTimeout on MDN

You can enclose the hide in a call to setTimeout() .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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