0

I have narrowed down an earlier question I asked and found out what is wrong, but cannot solve it.

I have a variable that needs to be set within one event function BEFORE the next event fires.

This is not happening, because I retrieve the variable via an Ajax call. And even though I think I have all the ajax settings right (?) - it doesn't happen. I am using console logging in Firebug, and the console log with "2" in it gets logged BEFORE the other log entry that should be made first (with "1" in it).

It's part of a datepicker instance, with an ajax call. Full code below. By the way - I have taken the ajax call out, and inserted a hard-coded variable to test - and it works perfectly. So it is the ajax that is not working correctly.

The variable 'varDate' is declared outside the document ready function:

<script type='text/javascript'>
 var varDate = 'toby';
$(document).ready(function(){

... and the main code here

$('.subject-date-pick').datetimepicker({
    timeFormat: 'hh:mm:ss',
    startDate:'01/01/1996',
    clickInput:true,
    beforeShow: function(input,inst) {

        var source = $("#" + inst.id).attr('source');
        var source_id = $("#" + inst.id).attr('source_id');
        var thing = $.ajax({
            type: 'GET', 
            url: 'ajax_get_unavailable_dates.php',
            dataType: 'text',
            async: 'false',
            global: 'false',
            data: {
                source_type: source,
                source_db_id: source_id
                  },
            success: function(msg) {


                console.log("1 - got the result");

                varDate = (msg)
            }
        });
        console.log("2 - ajax complete - logging console");     
        console.log("Post Ajax"+varDate);

    },

What the console does here is log the variable incorrectly first (with the SECOND logging event), and then the next event for the datepicker fires. After that, the variable IS updated - and subsequent clicks on the datepicker field will have the corrrect variable set - but this is too late and no good.

So: Console results:

GET request for Ajax call

2 - ajax complete - logging console

Post Ajaxtoby

1 - got the result

...

Anyone help?

2 Answers 2

1

I'm guessing that this is because 'false' == true :)

Try again with async: false; as the $.ajax ajax options.

1
  • Wow - that's gotta set a record for being dumb. Thanks!
    – Toby Behan
    May 23, 2012 at 0:35
0

Try async:false instead of async:'false'

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.