0

这段代码有两个问题。

1$(container + ' meta[data-level="' + level + '"]').length == 0始终为零我知道这一点,因为我创建了create_views(1);不应添加的额外调用,但无论如何它都会被附加。

2 dpUniSlider 不起作用,因为它看不到通过 ajax 调用创建的 li。如果我将它移到成功消息中,它可以正常工作,但外部功能不能。问题是如果我将它包含在 ajax 成功中,它将被调用多次,因为它在循环下

//Show levels using ajax, before slider is activated

function create_views(level) {
  $.ajax({
    url: "actions.php",
    type: "GET",
    data: "show_level=" + level,
    cache: false,
    success: function (data) {
      var view = '<li data-level="' + level + '">' + data + '</li>';
      var container = ".slides_container";
      if ($(container + ' meta[data-level="' + level + '"]').length == 0) {
        $(container).append(view);
      } else { //check if element exists, if yes overwrite it.
        //$(container + ' meta[data-level="' + level + '"]').replaceWith(view);
        alert("Exists");
      }
    }
  });
}
 //Loop through all levels and display views
 //level count can be rewritten to come from DB and not be hardcoded like now
var levels = 2;
for (var i = 1; i <= levels; i++) {
  create_views(i);
} // for loop
create_views(1); //test, delete this
 //Activate slide
var unislider = $(".slides_container").dpUniSlider({
  //loop: false,
  draggable: false
});
4

3 回答 3

1

我相信您可以使用 jQuery 延迟对象来处理多个并行异步 ajax 调用,您希望在它们全部成功完成后执行某些操作。

尝试这个:

$(function() {
    var LEVELS = 2,
        $container = $('.slides_container'),
        deferreds = []; // This will hold the deferred objects.

    // This function returns a deferred object.
    function getViewAndReturnDeffered(level) {
        return $.ajax({
            url: 'actions.php',
            type: 'GET',
            data: 'show_level=' + level,
            cache: false,
            success: function(data) {
                var $currentView = $container.find('[data-level="' + level + '"]'),
                    $newView = '<li data-level="' + level + '">' + data + '</li>';
                if ($currentView.length > 0) {
                    $currentView.replaceWith($newView);
                } else
                    $container.append($newView);
                }
            }
        });
    }

    for (var i = 1; i <= LEVELS; i++) {
        // Put the deferred objects in the array.
        deferreds.push(getViewAndReturnDeffered(i));
    }

    // The function passed to `.done()` will execute when all the deferred
    // objects have completed successfully.
    $.when.apply($, deferreds).done(function() {
        var unislider = $container.dpUniSlider({
            //loop: false,
            draggable: false
        });
    });
});
于 2013-03-25T18:13:21.740 回答
1

为了处理多个并行异步 ajax 调用,您希望在它们全部完成后执行某些操作,您可以保留一个计数器并检查成功回调以查看最后一个 ajax 调用何时成功。

它看起来像这样:

$(function() {
    var TOTAL_TASKS = 2,
        completedTasks = 0;

    function performAjaxTask(taskNumber) {
        $.ajax({
            url: url,
            type: 'GET',
            data: data,
            cache: false,
            success: function(data) {
                // Process the data

                completedTasks++;

                if (completedTasks == TOTAL_TASKS) {
                    // Perform actions that need to wait until all
                    // ajax calls have returned successfully.
                }
            }
        });
    }

    for (var i = 1; i <= TOTAL_TASKS; i++) {
        performAjaxTask(i);
    }
});
于 2013-03-25T15:55:51.957 回答
0

你有<meta></meta>内幕.container吗?可能不是。利用

$(container + ' li[data-level="' + level + '"]')
于 2013-03-25T15:49:59.853 回答