等到另一个ajax返回响应

时间:2013-09-05 05:02:57

标签: jquery ajax django

我在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需要一些时间,因此timetimetaken变量值将显示为未定义。

请告诉我如何解决这个问题。所以,一旦我收到getepoch的回复,那么只能继续进行。

由于

1 个答案:

答案 0 :(得分: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);      
    }
    });
}