-6

How to make a function to display after 5 or 6 seconds

    function a() {
alert("after 5 seconds");
}

a();
babuharry
  • 1
  • 4

2 Answers2

0

You can use all JS timers that you want.

setTimeout/setInterval, etc.

var duration = 5000; #in ms, miliseconds. 5000 = 5s

function delayedAlert() {
  window.setTimeout(slowAlert, 5000);
}

function slowAlert() {
  alert('That was really slow!');
}

Look more in https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Timers

capcj
  • 1,535
  • 1
  • 16
  • 23
  • 1
    You should not answer a duplicate. Its a bad practice – Rajesh Apr 07 '17 at 10:55
  • What is duplicate? I took the mdn example and adapted to him o.O Just being pragmatic, seriously? – capcj Apr 07 '17 at 10:56
  • 1
    @CarlosAlexandre I think he means don't answer a duplicate question, not that your answer is a duplicate. –  Apr 07 '17 at 11:11
  • @Orangesandlemons ahn, okay dudes, i was writing before him post, sry – capcj Apr 07 '17 at 11:14
  • 1
    @CarlosAlexandre it doesn't bother me, just explaining what he meant :-) –  Apr 07 '17 at 11:16
  • 1
    A duplicate is a post that has been asked on the portal before. When you come across a question that is basic question, there is a high possibility that its already been asked. Such posts have many answers and discussion. You should look for such posts and share their link instead. – Rajesh Apr 07 '17 at 11:18
  • Thank you, by now I will take care about that. – capcj Apr 07 '17 at 11:20
  • Hi @Rajesh i was looking for different method to solve that problem, i got it thank you and as you said i won't post duplicate question again. thanks for your time – babuharry Apr 08 '17 at 12:46
0

function delayer(ms){
  return new Promise((resolve, reject)=>{
    setTimeout(()=>{
      resolve();
    }, ms)
  })
}

async function myFunction(){  // Function Must be async.
  console.log("First Console")
  await delayer(3000);    // This Will Stop The Code For 3 Seconds
  console.log("Second Console After 3 Seconds")
}


myFunction()