title: How to wait for task to finish before moving on to next part of the code
body: <p>I have a "Save" button that I need to trigger when the user clicks Next/Previous buttons on my page. When the user clicks next to move onto another window I trigger a "save" button that saves the changed the user had mad and some other functionality. The problem is when I click the next or previous button the save button is triggered but needs time to save the changes and the code keeps on executing. Is there a way I can tell whether the save has finished or delay my code somehow? I know I can do a time delay but it could be different for every user and inefficient. Here is an example:</p>
  
  <pre><code>function prev_next(){
       $('#save').trigger('click');     //This line could take 2-5 seconds to finish
       //Other code here that opens next window
  }
  </code></pre>
  
  <p>Any advice will be appreciated.</p>