在某些情况下捕获正确天数的功能

时间:2017-01-22 02:52:31

标签: javascript

我希望这个问题有意义。我的javascript相当弱。但是,我有一个脚本用于通过apikey从我们的健身房软件中提取信息。基本上,每天晚上7点,如果锻炼已经发布,我希望脚本抓住它。如果是在晚上7点之前或晚上7点之后,明天的锻炼不可用,只需保持前一天锻炼。

脚本似乎在大多数时间都有效,但是在中午之前遇到问题。例如,今天上午10:45,该脚本应该已经捕获了1月21日的锻炼,但由于某种原因,它正在达到错误代码条款并保持锻炼从2017年1月1日。

对不起文字墙,但基本上希望有人可以告诉我/我的脚本中是否有问题。我认为问题所在的领域始于“今天=新的日期()”

谢谢

    <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" />

1 个答案:

答案 0 :(得分:0)

$.ajax()是异步的,这意味着您的return "worked";实际上并非由loadFormattedWOD()返回。要解决此问题,请使用retryWithToday作为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");
}