Click here to Skip to main content
15,898,902 members
Home / Discussions / JavaScript
   

JavaScript

 
Questionsubmit button Enable Disable Function Using Javascript and Ajax with Html and CSS Pin
Member 1297691031-Jan-17 20:17
Member 1297691031-Jan-17 20:17 
QuestionRe: submit button Enable Disable Function Using Javascript and Ajax with Html and CSS Pin
ZurdoDev1-Feb-17 1:57
professionalZurdoDev1-Feb-17 1:57 
Questionangular translate problem Pin
Member 1103130431-Jan-17 20:13
Member 1103130431-Jan-17 20:13 
AnswerRe: angular translate problem Pin
Nathan Minier1-Feb-17 1:24
professionalNathan Minier1-Feb-17 1:24 
GeneralRe: angular translate problem Pin
Member 110313041-Feb-17 2:08
Member 110313041-Feb-17 2:08 
AnswerRe: angular translate problem Pin
Nathan Minier1-Feb-17 2:17
professionalNathan Minier1-Feb-17 2:17 
QuestionAJAX javascript Query Not executing properly Pin
Jason Nielsen24-Jan-17 14:18
Jason Nielsen24-Jan-17 14:18 
AnswerRe: AJAX javascript Query Not executing properly Pin
F-ES Sitecore24-Jan-17 23:04
professionalF-ES Sitecore24-Jan-17 23:04 
There are a few issues with your code as it is, look at my comments below

JavaScript
today = new Date();
// you don't need the else, it isn't doing anything
if (today.getHours() >= 19) { today.setDate(today.getDate() + 1); }
else { today.setDate(today.getDate() + 0); }
var dateString = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();

// first off you have two loadFormattedWOD functions defined above so which one is your code going to use?
// function names have to be unique
if (loadFormattedWOD("#wodbody", "myapikey", dateString, "MYGYM", "CrossFit") == null) {
    // loadFormattedWOD makes an asynchronous call to the api.  What this means is that the call happens
    // "in the background", so one the ajax call is made the code does not wait for the response to come
    // back so your function exists immediately, it does not wait for "success" to be called so your
    // function always returns null so this "if" will always be true.

    // you call retryWithToday upon a failure but what if the date wasn't amended due to the hour?
    // in that case you're actually using yesterday.
    retryWithToday();
}

// don't embed functions mid-code
function retryWithToday() {
    today.setDate(today.getDate() - 1);
    var dateString = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
    loadFormattedWOD("#wodbody", "myapikey", dateString, "MYGYM", "CrossFit");
}

// you call the function here again
loadFormattedWOD("#wodbody", "myapikey", dateString, "MYGYM", "CrossFit");


I've re-arranged your code a little, I don't have a valid api key so I can't check the code works etc but this is more the direction you should be heading

JavaScript
// only one function, remove the other one
function loadFormattedWOD(selector, apiKey, date, location, program, callback) {
    $.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);
                    console.log('worked');
                }
                else {
                    if (callback) callback();
                }
            } catch (err) {
                if (callback) callback();
            }
        }
    });
}

function retry() {
    console.log('In retry');
    // we'll only get here if the ajax call returns a failure
    var today = new Date();

    // if it is after 7 try for today's date instead
    if (today.getHours() >= 19) {
        var dateString = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
        loadFormattedWOD("#wodbody", "myapikey", dateString, "MYGYM", "CrossFit", function () {
            // here I'm passing an in-line function as the callback so this
            // code will get called on failure
            console.log("failed again");
            // update the page to say something like "Try again later"
        })
    }
    else {
        console.log("didn't retry");
        // update the page to say something like "Try again later"
    }
}

today = new Date();
if (today.getHours() >= 19) { today.setDate(today.getDate() + 1); }

var dateString = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();

// pass the "retry" function as the callback.
loadFormattedWOD("#wodbody", "myapikey", dateString, "MYGYM", "CrossFit", retry)

// loadFormattedWOD will now update the html on success, and on failure it will call
// retry and that is where you handle the failure case

PraiseRe: AJAX javascript Query Not executing properly Pin
Jason Nielsen25-Jan-17 6:45
Jason Nielsen25-Jan-17 6:45 
AnswerRe: AJAX javascript Query Not executing properly Pin
F-ES Sitecore25-Jan-17 3:48
professionalF-ES Sitecore25-Jan-17 3:48 
GeneralRe: AJAX javascript Query Not executing properly Pin
Jason Nielsen28-Jan-17 17:06
Jason Nielsen28-Jan-17 17:06 
GeneralRe: AJAX javascript Query Not executing properly Pin
F-ES Sitecore29-Jan-17 22:00
professionalF-ES Sitecore29-Jan-17 22:00 
GeneralRe: AJAX javascript Query Not executing properly Pin
F-ES Sitecore30-Jan-17 23:01
professionalF-ES Sitecore30-Jan-17 23:01 
Questiondwg to jpg using javascript Pin
solynaseng19-Jan-17 4:50
solynaseng19-Jan-17 4:50 
AnswerRe: dwg to jpg using javascript Pin
Eddy Vluggen19-Jan-17 4:56
professionalEddy Vluggen19-Jan-17 4:56 
GeneralRe: dwg to jpg using javascript Pin
solynaseng19-Jan-17 5:32
solynaseng19-Jan-17 5:32 
AnswerRe: dwg to jpg using javascript Pin
Richard MacCutchan19-Jan-17 5:29
mveRichard MacCutchan19-Jan-17 5:29 
GeneralRe: dwg to jpg using javascript Pin
solynaseng19-Jan-17 5:34
solynaseng19-Jan-17 5:34 
GeneralRe: dwg to jpg using javascript Pin
Richard MacCutchan19-Jan-17 5:42
mveRichard MacCutchan19-Jan-17 5:42 
GeneralRe: dwg to jpg using javascript Pin
solynaseng19-Jan-17 6:00
solynaseng19-Jan-17 6:00 
GeneralRe: dwg to jpg using javascript Pin
dandy7219-Jan-17 6:40
dandy7219-Jan-17 6:40 
GeneralRe: dwg to jpg using javascript Pin
Rajesh R Subramanian19-Jan-17 12:38
professionalRajesh R Subramanian19-Jan-17 12:38 
GeneralRe: dwg to jpg using javascript Pin
dandy7219-Jan-17 13:42
dandy7219-Jan-17 13:42 
GeneralRe: dwg to jpg using javascript Pin
Richard MacCutchan19-Jan-17 22:26
mveRichard MacCutchan19-Jan-17 22:26 
GeneralRe: dwg to jpg using javascript Pin
Rajesh R Subramanian20-Jan-17 0:01
professionalRajesh R Subramanian20-Jan-17 0:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.