1

I have a button which when clicked performs myFunction with two actions, i'd like it to perform the first action, wait for 2 seconds, then perform the action. This is the code so far:

function myFunction() {
        document.getElementById('box1').style.display = "block"; $("#box2").data( "mmenu" ).close();
};

Thanks!

user2015431
  • 35
  • 1
  • 4
  • Please refer to http://stackoverflow.com/questions/17883692/how-to-set-time-delay-in-javascript – Jerry Jul 10 '16 at 12:02
  • Possible duplicate of [Sleep in Javascript - delay between actions](http://stackoverflow.com/questions/758688/sleep-in-javascript-delay-between-actions) – Aleksey L. Jul 10 '16 at 12:05

1 Answers1

1

Use setTimeout for providing delay

function myFunction() {
  document.getElementById('box1').style.display = "block";
  setTimeout(function() {
    // code to execute after delay
    $("#box2").data("mmenu").close();
  }, 2000);
  // -^- delay in milliseconds 
};
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188