Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 738
Satch3000 Avatar asked Feb 16 '12 16:02

Satch3000


People also ask

How do you make a function wait for 5 seconds?

Usage: const yourFunction = async () => { await delay(5000); console. log("Waited 5s"); await delay(5000); console.

How do you do a 1 second delay in JavaScript?

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.

How do you sleep 5 seconds in JavaScript?

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.

How do you wait 1 second in HTML?

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');


1 Answers

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...)

like image 126
gdoron is supporting Monica Avatar answered Sep 28 '22 08:09

gdoron is supporting Monica