0

我在 Django 中发出 AJax 请求,例如

      function getepoc(epochtime){

        //alert(epochtime);
        $.ajax({
            beforeSend: before_send_csrf,
            url : CHANGE_TO_EPOCH_TIME,

            type : "POST",
            cache : false,

            dataType: "json",
            data: "etime="+epochtime,
            success: function (data){
                    alert('converted '+ data);
                    return data;        

       }

     });}

      function getdetails(todoid,type){
        $('#dynamic_data').empty();
        $('#ajax_loader').addClass('in').removeClass('hide');
        if(type == 'Certification')
        {
            HISTORY_DETAIL = CERTIFICATION_HISTORY_DETAILS;
        }
        $.ajax({
            beforeSend: before_send_csrf,
            url : HISTORY_DETAIL,
            type : "POST",

            cache : false,
            //dataType: "json",
            data: JSON.stringify(get_history_todoid(todoid)),
            success: function (data){
                    var historyItem = data.result;
                    var htmlData="";
                    if( historyItem.length == 0) {
                        htmlData += "<tr><td>No Records</td></tr>"
                    } 
                    else{
                        for(var i=0; i<historyItem.length; i++)
                        {
                            var str = "'"+historyItem[i].id+"'";
                            //timeStarted = getFormattedDatetime(historyItem[i].timestarted);
                            //timeStarted = getepoc(historyItem[i].timestarted);

                            //alert('datattttttt' + timeStarted);
                            if(historyItem[i].lastupdate != null)
                            {
                                lastupdate =historyItem[i].lastupdate;
                            //  alert(lastupdate);
                                timetaken = getepoc(lastupdate);                                        
                            }
                            else{
                                timetaken = 'N/A'
                            }
                            var time = getepoc(historyItem[i].timestarted);
                            console.log(time);
                            htmlData +=
                                 "<tr><td>"+historyItem[i].topicname+"</td>"+                                   
                                    "<td>"+historyItem[i].status+"</td>"+
                                    "<td>"+time+"</td>"+
                                    "<td>"+timetaken+"</td>"+
                                    "</tr>";

                        /*          "<td><a href='#modal' data-toggle='modal' onclick='modalDialog(\"" + historyItem[i].name + "\",\"" + historyItem[i].timetaken + "\",\"" + historyItem[i].passed + "\",\"" + timeStarted + "\",\"" + lastUpdated + "\");'>Details</a></td>";
                        */
                        }
                    }                           
                        $('#dynamic_data').append(htmlData);
                        $('#ajax_loader').removeClass('in').addClass('hide');    

                    callBackModal();
                },
                error : function (err)
                {
                    var htmlData="";
                    htmlData +="Something went wrong please try later ";                        
                    $('#dynamic_data').append(htmlData);
                    $('#ajax_loader').removeClass('in').addClass('hide');    
                }



        });
      }

最初getdetails 正在执行,然后我正在执行另一个函数调用getepoch 。在 getepoch 中,我正在发出第二个 Ajax 请求。

我的问题是 getepoch 函数 Ajax 需要一些时间,因此time变量timetaken值出现为 undefined 。

请告诉我如何解决这个问题。这样一旦我得到 getepoch 的回复,就只能继续进行。

谢谢

4

1 回答 1

1

这是 javascript - 使用回调!

getepoc(historyItem[i].timestarted, function(time) {
    console.log(time);
    htmlData +=
        "<tr><td>"+historyItem[i].topicname+"</td>"+                                   
            "<td>"+historyItem[i].status+"</td>"+
            "<td>"+time+"</td>"+
            "<td>"+timetaken+"</td>"+
        "</tr>";
});

function getepoc(epochtime, callback) {
    $.ajax({
        beforeSend: before_send_csrf,
        url : CHANGE_TO_EPOCH_TIME,

        type : "POST",
        cache : false,

        dataType: "json",
        data: "etime="+epochtime,
        success: function(data) {
            alert('converted '+ data);
            callback(data);      
    }
    });
}
于 2013-09-05T07:09:38.310 回答