【问题标题】:Loading an XML file with todays date in the filename... need to fallback to old file if one with todays date doesnt exist加载文件名中包含今天日期的 XML 文件...如果不存在包含今天日期的文件,则需要回退到旧文件
【发布时间】:2011-03-01 06:39:42
【问题描述】:

所以这是我的问题。使用 javascript/jQuery 我目前正在加载一个 XML 文件,该文件的文件名如 carousel_large_2010-06-08.xml .. 我这样做的方式是检查今天的日期,然后抓取一个文件名中包含该日期的文件...问题是有时他们不会在某一天上传新文件,因此它需要回退到存在的旧日期。想知道该怎么做?这是我的代码:

        // 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');
            }
        }); 

【问题讨论】:

    标签: javascript jquery xml ajax datetime


    【解决方案1】:

    这是一个建议的(未经测试的)解决方案。我主要基于您的,但排除了日期字符串计算。将maxOffset 设置为您想回顾的最大天数(在您昨天说的问题中,所以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');
    }
    

    【讨论】:

    • 这将始终只尝试一个请求,因为在 AJAX 调用返回之前,success 无法再次设置为 0(此时循环早已结束)。跨度>
    • @VoteyDisciple - 哎呀...忘记设置异步...完成。好皮卡。
    • 这似乎不起作用......它没有给出任何错误......轮播只是停止工作......它似乎根本没有进入 for 循环......我发出警报那里没有得到回应
    【解决方案2】:

    我假设在 AJAX 调用返回之前您不知道文件是否丢失,因此您可以尝试再次查找。

    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);
    

    【讨论】:

    • 如果文件根本不存在,这可能会出现无限循环(这就是您所说的回退机制吗?)。如果查找必须足够长,它也可能是 StackOverflow - 没有双关语。请参阅novemberborn.net/2007/08/javascriptcallstack-size-120 了解限制
    • 我同意。我确实建议建立一个“纾困”机制。我继续添加了一个。
    • 由于回避过多,仍然存在 SO 问题,但对于 1 天的回溯查找,这不是问题。
    • 不,最多只能递归三次。如果attempt 达到 3(在我的示例中),它将返回而不尝试任何进一步的查找。
    • 抱歉,我的意思是如果允许的尝试次数不是 3 而是 3000+(10 年),那么根据浏览器,您处于 SO 领域
    猜你喜欢
    • 2022-12-24
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多