-5

My code currently console.logs the value of the variable timestamp when the document is ready. Is there a way of running this every time the variable timestamp is updated in the .datepicker() function without it being in the function itself?

$("document").ready(function() {
  var timestamp;
  $("#startDate").datepicker({
    onSelect: function(e) {
      var dateAsObject = $(this).datepicker("getDate");
      timestamp = dateAsObject.getTime();
      console.log("user selected: " + timestamp);
      return timestamp;
    }
  });
  console.log("test: " + timestamp);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<form action="">
  <label for="startDate">Select a date:</label>
  <input type="text" id="startDate" name="startDate">
</form>
James Douglas
  • 2,948
  • 2
  • 18
  • 38
WebDevBooster
  • 13,159
  • 8
  • 57
  • 66
  • @JamesDouglas "urgent"? Nowhere did I say anything about "urgent". I said that I spent hours battling this issue and am still no closer to a solution. That's what I said. Has nothing to do with "urgent" whatsoever! – WebDevBooster Oct 14 '17 at 15:31
  • You can't access it until the event occurs. What are you trying to do outside the event callback? – charlietfl Oct 14 '17 at 15:31
  • @charlietfl I understand that much. But can't figure out the solution... – WebDevBooster Oct 14 '17 at 15:32
  • Solution to what? You haven't explained the higher level problem – charlietfl Oct 14 '17 at 15:33
  • @JamesDouglas OK, I removed the first line. – WebDevBooster Oct 14 '17 at 15:34
  • You can't access it there.... When your code is running, it will not wait for the date to be picked, therefore you get `undefined`. You need to specify the action in your event-handler, and let it call some function where you can have something happen. – some Oct 14 '17 at 15:34
  • @charlietfl the problem is explained in the code comments. (I thought putting it into the code comments would help making it clear) – WebDevBooster Oct 14 '17 at 15:35
  • @some thank you, I understood that part already, but still no closer to the actual solution. – WebDevBooster Oct 14 '17 at 15:36
  • No .... writing it in paragraph text makes it clear as well as searchable – charlietfl Oct 14 '17 at 15:38
  • Possible duplicate of [Listening for variable changes in JavaScript or jQuery](https://stackoverflow.com/questions/1759987/listening-for-variable-changes-in-javascript-or-jquery) – James Douglas Oct 14 '17 at 15:40
  • @JamesDouglas Thank you for updating my question text! I seems I have yet to learn to properly formulate my questions. Will take your edit as a template. Now looking into the thread you linked to... – WebDevBooster Oct 14 '17 at 15:45

1 Answers1

1

I could be misunderstanding what you're asking, but couldn't you just call the other function from within the onSelect event handler?

Here's a JSFiddle showing this in action, which I believe is doing what you need.

$("document").ready(function() {
    var timestamp;
    $("#startDate").datepicker({
        onSelect: function(e) { 
            var dateAsObject = $(this).datepicker( "getDate" ); 
            timestamp = dateAsObject.getTime(); 
            console.log("user selected: " + timestamp);
            otherFunction();
        }
    });

    function otherFunction() {
        console.log("test: " + timestamp);
    }
});
Nick
  • 12,032
  • 3
  • 11
  • 27
  • And get rid of `return` since there is nothing to return to – charlietfl Oct 14 '17 at 15:33
  • @snapjs No, I can't call it from within there. Because that other function is handled via a different jQuery plugin. (I had certainly tried that before) – WebDevBooster Oct 14 '17 at 15:39
  • Better to use `otherFunction(timestamp)` instead of using the semi-global `timestamp`. You can probably shorten that function to `otherFunction($(this).datepicker( "getDate" ).getTime())` – some Oct 14 '17 at 15:39
  • @WebDevBooster so what's the other function that needs this timestamp? Can't you just call *that* function? I think we need a bit more info to help you out! – Nick Oct 14 '17 at 15:42
  • @snapjs I deliberately posted the question without that other function because I need to understand how to solve this issue in general. I need to know how to solve this issue whenever in comes up. – WebDevBooster Oct 14 '17 at 15:47