【问题标题】:Is there any way to use jQuery's deferred / promise / when / then features for multiple ajax calls in a loop?有什么方法可以在循环中使用 jQuery 的 deferred / promise / when / then 功能进行多个 ajax 调用?
【发布时间】:2012-01-27 06:19:56
【问题描述】:

我一直在阅读更新的 jQuery 功能如何使用 Deferred / promise / when / then 来简化对多个异步事件(例如 AJAX 查询)的操作。

但我一直在阅读的所有示例都讨论了一组预先知道的查询。

$.when($.ajax("/page1.php"), $.ajax("/page2.php")).then(myFunc, myFailure);

(借自Coding Freak's answer here

如果你事先不知道它们怎么办,例如在循环中使用不同的参数调用相同的分页 Web API,例如 MediaWiki 和 StackExchange API 的某些功能 - 有什么办法可以利用在这种情况下这些新的 jQuery 特性还是我们必须回到旧的方式?

【问题讨论】:

标签: jquery loops jquery-deferred promise


【解决方案1】:

你可以调用类似ajaxloop('/page1.php')的函数

function ajaxloop(url)
{
    $.ajax({
            url:url,
            success:function()
                       {
                     if(url=='/page1.php')
                      {
                       ajaxloop('/page2.php')
                      }
                     else
                     {
                      ajaxloop('/page1.php')
                       }
                 }
     });
}

【讨论】:

  • 给我一个具体的问题,你是否尝试循环你的 jquery。
  • 看来您需要提前知道确切的迭代次数,并为这些确切的迭代包含内联代码。
猜你喜欢
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多