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();
Usage: const yourFunction = async () => { await delay(5000); console. log("Waited 5s"); await delay(5000); console.
To delay a function execution in JavaScript by 1 second, wrap a promise execution inside a function and wrap the Promise's resolve() in a setTimeout() as shown below. setTimeout() accepts time in milliseconds, so setTimeout(fn, 1000) tells JavaScript to call fn after 1 second.
Create a Simple Delay Using setTimeout console. log("Hello"); setTimeout(() => { console. log("World!"); }, 5000); This would log “Hello” to the console, then make JavaScript wait 5 seconds, then log “World!” to the console.
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');
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...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With