title: datepicker.js update date when date is updated by user
body: <p>I am using a library called  <a href="https://bootstrap-datepicker.readthedocs.io/en/stable/" rel="nofollow noreferrer">bootstrap-datepicker.js</a> (not using the latest version though).</p>
  
  <p>I have a calendar input which I want to update to the closest monday once the user has picked a date. To be able to do that I have the following code:</p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;div class="form-group" id="data"&gt;
      &lt;div class="input-group date"&gt;
          &lt;input id="control-monday" value="01/01/2000" type="text"&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code>$('#data .input-group.date').datepicker({
      startView: 1,
      todayBtn: "linked",
      keyboardNavigation: false,
      forceParse: false,
      autoclose: true,
      format: "dd/mm/yyyy",
      weekStart: 1,
      endDate: "0d"
  }).on('changeDate', DateUpdated);
  
  
  function DateUpdated(e) {  
  
      PickedDate = e.date; 
      //Finding monday
      monday = ....;
  
      $("#control-monday").datepicker("setDate", monday);
      $("#control-monday").val(monday);
  
  
  }
  </code></pre>
  
  <p>The issue is that every time I update the date to the closest monday using DateUpdated(), the function is fired again since the date was updated and becomes a infinite loop. Is there a way to only fire the function DateUpdated() only when a user  manually picked a date and not when Jquery does it?</p>
  
  <p>to explain a bit farther of what is happening:</p>
  
  <ol>
  <li>User pick a date</li>
  <li>DateUpdated() is called due to <code>.on('changeDate', DateUpdated);</code></li>
  <li>The date is updated to the closest monday <code>$("#control-monday").datepicker("setDate", monday);</code></li>
  <li>Because the date was changed in point 3 DateUpdated() is called AGAIN due to <code>.on('changeDate', DateUpdated);</code></li>
  <li>3 and 4 go for ever.</li>
  </ol>
  
  <p>I am currently using a global variable to count how many times the function has been called and only firing it when is the first time and restarting the count after 2 calls but it's a bit ugly and very unstable. </p>