Do action when two buttons are clicked one after another

Do action when two buttons are clicked one after another

Hello I am trying to make an alert when two buttons are clicked one after another but I have a problem, the following code doesn't work

  1. <script>
  2. var hasBeenClicked = false;
  3. $(document).ready(function() {
  4.     $("#submit1").click(function(){
  5.         hasBeenClicked = true;
  6.     }); 
  7.  var hasBeenClicked2 = false;
  8.    $("#submit2").click(function(){
  9.         hasBeenClicked2 = true;
  10.     }); 
  11.    if(hasBeenClicked==true && hasBeenClicked2==true){
  12.     alert('Alert');
  13.    }
  14. });
  15. </script>

  16. <body>
  17. <button type="button" id="submit1">Click Me!</button>
  18. <button type="button" id="submit2">Click Me!</button>

  19.  
  20.  
  21. </body>


I don't understand why it doesn't work and is there any other more optimal solution to the code I wrote?