$ .getJSON请求不会运行,但下一行代码会运行

时间:2013-03-07 04:21:27

标签: javascript jquery

我有一个$ .getJSON请求,但是在请求之后没有运行,而是代码行。如果我在$ .getJSON请求之后删除所有代码,则运行请求。如何获取对返回数据进行迭代的请求,然后根据请求运行代码。

var eventList = new Array();
$.getJSON('../index.php?/home/events', function(eventItems){    
    $.each(eventItems, function() {
        var event = this;
        var eventItem = new Array();
        // format the date and append to span
        eventItem[0] = formatMDYDate(formatTimeStamp(this.loc_datetime, false), 0);
        // add shortdescription to div
        eventItem[1] = this.shortdescription; 

        // check if longdescription exist
        if (this.longdescription) {
            // create new anchor element for "More Info" link on events
            var link = $('<a></a>');
            link.attr('href', '../index.php?/home/event_info');
            link.addClass('popup');
            link.html('More Info');
            //link.bind('click', eventPopUp());
            link.bind('click', function() {
                var addressValue = event.id;
                dialog = $('<div></div>').appendTo('body');
                dialog.load('../index.php?/home/event_info', 
                    {id: addressValue});
                dialog.modal({
                    opacity: 80
                });
                return false;
            });
            eventItem[2] = link;
        }
        eventList.push(eventItem);
    });  
});
// removing the following lines of code will let the .getJSON request run
if (eventList.length > 0) {
    write_Events(eventList);
}

我不知道造成这个问题的原因,请帮忙!

4 个答案:

答案 0 :(得分:4)

异步意味着当你调用它时,JS运行时不会等到它在执行下一行代码之前完成。通常,在这种情况下您需要使用回调。

它类似于:

var a="start";
setTimeout(function(){
 a="done";
 dosomethingWithA(a);
},1000);
if(a=="done"){}//doesn't matter, a is not "done"
function dosomethingWithA(a){
 // a is "done" here
}

在您的情况下,代码应该类似于:

var eventList = new Array();
$.getJSON('../index.php?/home/events', function(eventItems){    
    $.each(eventItems, function() {
        var event = this;
        var eventItem = new Array();
        // format the date and append to span
        eventItem[0] = formatMDYDate(formatTimeStamp(this.loc_datetime, false), 0);
        // add shortdescription to div
        eventItem[1] = this.shortdescription; 
        // check if longdescription exist
        if (this.longdescription) {
            // create new anchor element for "More Info" link on events
            var link = $('<a></a>');
            link.attr('href', '../index.php?/home/event_info');
            link.addClass('popup');
            link.html('More Info');
            //link.bind('click', eventPopUp());
            link.bind('click', function() {
                var addressValue = event.id;
                dialog = $('<div></div>').appendTo('body');
                dialog.load('../index.php?/home/event_info', 
                    {id: addressValue});
                dialog.modal({
                    opacity: 80
                });
                return false;
            });
            eventItem[2] = link;
        }
        eventList.push(eventItem);
    });
    processEventList();  
});
function processEventList(){
  // removing the following lines of code will let the .getJSON request run
  if (eventList.length > 0) {
    write_Events(eventList);
  }
}

答案 1 :(得分:3)

尝试

var eventList = new Array();
    $.getJSON('../index.php?/home/events', function (eventItems) {
        $.each(eventItems, function () {
            //....
            eventList.push(eventItem);               
        });
        // removing the following lines of code will let the .getJSON request run
        if (eventList.length > 0) {
            write_Events(eventList);
        }
    });

或者,您可以将PubSub与jquery技术一起使用

 var eventList = new Array();
    $.getJSON('../index.php?/home/events', function (eventItems) {
        $.each(eventItems, function () {
           //....
            eventList.push(eventItem);               
        });            
        //publisher
        $(document).trigger('testEvent', eventList);
    });

    //subscriber
    $(document).bind("testEvent", function (e, eventList) {
        if (eventList.length > 0) {
            write_Events(eventList);
        }
    });

更多详情http://www.codeproject.com/Articles/292151/PubSub-with-JQuery-Events

快乐编码.. :)

答案 2 :(得分:1)

$.getJSON是异步调用。直到当前函数完全执行后才会执行回调。调用后的代码将始终在getJSON回调运行之前运行。

write_Events函数可能会抛出错误并停止执行,这就是回调永远不会运行的原因。或者它实际上正在运行,但是由于额外代码调用的任何原因,你没有看到它的证据。

答案 3 :(得分:0)

javascript代码永远不会等待来自服务器的响应,我们需要停止处理javascript,直到我们收到服务器的响应。

我们可以使用jquery.Deferred

来完成此操作

您还可以访问this教程。