2

So here is my question. Using javascript/jQuery I am currently loading in an XML file that has a file name such as carousel_large_2010-06-08.xml.. the way I am doing it is checked for todays date then grabbing a file that has that date in the filename... the issue is sometimes they wont be uploading a new file for a given day so it needs to fallback to a older date that exists.. Wondering how to do it? Here is my code:

        // set date for xml file
        var currentTime = new Date(),
            month = currentTime.getMonth() + 1,
            day = currentTime.getDate(),
            year = currentTime.getFullYear();

        if(month.toString().length == 1){
            month = '0'+month.toString();
        }
        if(day.toString().length == 1){
            day = '0'+day.toString();
        }

        var dateObject = year+"-"+month+"-"+day;

        // start magic
        $jq.ajax({
            type: "GET",
            url: "_xml/carousel/home/carousel_large_"+dateObject+".xml",
            dataType: "xml",
            success: HPCarousels.heroCarousel.parseXML,
            error: function(){
                alert('Error Loading XML Content');
            }
        }); 

2 Answers 2

1

Here's a proposed (untested) solution. I based it largely on yours, but factored out date string calculation. Set maxOffset to the max # of days you want to look back (in your question you said yesterday, so 1)

function getDateString(offset) {

    // set date for xml file
    var currentTime = new Date().setDate(today.getDate()-offset),
        month = currentTime.getMonth() + 1,
        day = currentTime.getDate(),
        year = currentTime.getFullYear();

    if(month.toString().length == 1){
        month = '0'+month.toString();
    }
    if(day.toString().length == 1){
        day = '0'+day.toString();
    }

    return year+"-"+month+"-"+day;
}

var maxOffset = 1;
var success = 0;
for(var offset = 0; offset <= maxOffset && !success; offset++) {
    success = 1;
    // start magic
    var dateString = getDateString(offset);
    $jq.ajax({
        type: "GET",
        async: false; 
        url: "_xml/carousel/home/carousel_large_"+dateString+".xml",
        dataType: "xml",
        success: HPCarousels.heroCarousel.parseXML,
        error: function(){
            success = 0;

        }
    }); 
}
if (!success) {
    alert('Error Loading XML Content');
}
3
  • That will always attempt only one request, since success can't get set to 0 again until the AJAX call returns (by which point the loop is long-ago concluded). Jun 8, 2010 at 17:01
  • @VoteyDisciple - yuk... forgot to set async... done. Good pickup.
    – DVK
    Jun 8, 2010 at 17:07
  • this doesnt seem to work... it doesnt give any errors.. the carousel just stops working... it doesnt seem to be going into the for loop at all.. i stuck an alert in there and didnt get a response
    – user361976
    Jun 8, 2010 at 18:20
0

I'm assuming you don't know if the file is missing until the AJAX call returns, so at that point you can attempt another lookup.

function getDate(timestamp) {
    month = timestamp.getMonth() + 1,
    day = timestamp.getDate(),
    year = timestamp.getFullYear();

    return year + '-' + ((month < 10) ? '0' + month : month) + '-' + ((day < 10) ? '0' + day : day);
}

function attemptGet(timestamp, attempt) {
    if (attempt >= 3) // Maximum number of attempts
        return;
    $jq.ajax({
        type: "GET",
        url: "_xml/carousel/home/carousel_large_"+dateObject+".xml",
        dataType: "xml",
        success: HPCarousels.heroCarousel.parseXML,
        error: function(){
            if (/* file is missing */) {
                attemptGet(timestamp - 24 * 60 * 60, attempt + 1);
            } else {
                alert('Error Loading XML Content');
            } 
        }
    }); 
}
attemptGet(0);
5
  • This has a possibility of infinit loop if the file's not there at all (is that what you meant by fallback mechanism?). It might also StackOverflow if the lookup must be long enough - no pun intended. See novemberborn.net/2007/08/javascriptcallstack-size-120 for limits
    – DVK
    Jun 8, 2010 at 17:02
  • I agree. I did suggest building in a "bail out" mechanism. I went ahead and added one. Jun 8, 2010 at 17:04
  • still have SO problem from too much recusion, though for 1-day-back lookup it's not an issue.
    – DVK
    Jun 8, 2010 at 17:08
  • No, that will only recurse up to three times. If attempt gets as high as 3 (in my example) it will return without attempting any further lookups. Jun 8, 2010 at 17:10
  • sorry, I meant if the allowed # of attempts is not 3 but 3000+ (10 years) - then you are in SO territory depending on the browser
    – DVK
    Jun 8, 2010 at 20:12

Your Answer

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