-1

I hope this question will make sense. My javascript is fairly weak. However, I have a script used to pull information from our gyms software via apikey. Basically, at 7PM every night, if the workout has been posted I want the script to grab it. If it is prior to 7PM or after 7PM and tomorrow's workout isn't available, just keep previous days workout.

The script appears to work the majority of the time, however are running into issues when it is prior to noon. For instance, today, at 10:45AM, the script should have captured workout for 1/21/2017, but for some reason it was hitting the error code clause and keeping workout from 1/20/2017.

Sorry for wall of text, but basically was hoping someone may be able to tell me what/if there is a problem in my script. The area where I think the issue lies starts with the "today = new Date()"

Thank You

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
<script> 
    function loadFormattedWOD(selector, apiKey, date, location, program) { 
        $.ajax({ 
            url: 'https://app.wodify.com/API/WODs_v1.aspx', 
            data: {
                apiKey: apiKey, 
                date: date, 
                location: location, 
                program: program, 
                type: "json"
            },  
            dataType: "json",  
            success: function(data){
                try {
                if(data && data.RecordList.APIWod.FormattedWOD) {  
                  $(selector).html(data.RecordList.APIWod.FormattedWOD);
                return "worked";

                }
                } catch(err) {
                return null;
                }               
            }
        });  
    }
function loadFormattedWOD2(selector, apiKey, date, location, program) { 
        $.ajax({ 
            url: 'https://app.wodify.com/API/WODs_v1.aspx', 
            data: {
                apiKey: apiKey, 
                date: date, 
                location: location, 
                program: program, 
                type: "json"
            },  
            dataType: "json",  
            success: function(data){  
                if(data && data.RecordList.APIWod.FormattedWOD) {  
                  $(selector).html(data.RecordList.APIWod.FormattedWOD);  
                }  
            }  
        });  
    }   

 today = new Date()
if (today.getHours() >=19) {today.setDate(today.getDate()+1);}
  else {today.setDate(today.getDate()+0);}
    var dateString = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate(); 
    if(loadFormattedWOD("#wodbody", "myapikey", dateString, "My Crossfit", "CrossFit") == null) {
    retryWithToday();
    }
function retryWithToday() {
today.setDate(today.getDate() - 1);
var dateString = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate(); 
    loadFormattedWOD2("#wodbody", "myapikey", dateString, "My Crossfit", "CrossFit");
}   
 </script> 
<div id="wodbody" />
  • According to [this answer](http://stackoverflow.com/a/4868270], the correct way to get tomorrow's date is `a = new Date((new Date()).valueOf() + 1000*3600*24);`. This prevents dates such as the 32nd day of the month. – tklg Jan 22 '17 at 03:12

1 Answers1

0

$.ajax() is asynchronous, meaning that your return "worked"; isnt actually returned by loadFormattedWOD(). To get around this, use retryWithToday as a callback to loadFormattedWOD():

function loadFormattedWOD(selector, apiKey, date, location, program, callback) { 
    $.ajax({ 
        ...
        success: function(data){
            try {
                if (data && data.RecordList.APIWod.FormattedWOD) {  
                    $(selector).html(data.RecordList.APIWod.FormattedWOD);
                    console.log('worked');
                }
            } catch(err) {
                if (callback) callback();
            }               
        }
    });  
}
...
loadFormattedWOD("#wodbody", "myapikey", dateString, "My Crossfit", "CrossFit", retryWithToday);
function retryWithToday() {
    today.setDate(today.getDate() - 1);
    var dateString = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate(); 
    loadFormattedWOD("#wodbody", "myapikey", dateString, "My Crossfit", "CrossFit");
}
tklg
  • 2,572
  • 2
  • 19
  • 24
  • Thank you Villa7_, that makes sense after seeing your iteration, much cleaner than what I was trying to accomplish. Very much appreciated. And I will adjust my date parameter as well to avoid any conflicts. – Jason Nielsen Jan 22 '17 at 03:29