【问题标题】:jQuery .each loop pausing for iframe to fully loadjQuery .each 循环暂停 iframe 以完全加载
【发布时间】:2013-05-13 00:17:28
【问题描述】:

我需要暂停 .each 循环,让 iframe 有时间加载。有没有办法做到这一点?我研究了回调等等,但没有运气。我不想超时,因为我需要确保 iframe src 完全加载。

$.ajax({
  type:'post',
  traditional: true,
  url: '/Reportedit/exportPdfCharts',
  dataType: 'json',
  cache: 'false',
  contentType: 'application/json; charset=utf-8',
  success: function(data) {

    $.each(data, function() {

        var url = "/Reportedit/createonechart";

        $("#iframe-chart").attr("src", url);

        //NEED TO PAUSE HERE UNTIL iframe source has completely loaded.


    });

  }
});

【问题讨论】:

  • 您可以在 iframe 上连接 onload 事件,但由于 ajax 调用是异步的,因此您不能在通话中暂停。如果您需要等待,那么 AJAX 可能不是正确使用的工具。
  • 你不能暂停 $.each 循环。

标签: jquery ajax iframe load each


【解决方案1】:

我假设,因为您的代码似乎显示了这种情况,您正在更新相同的 iframe 源,而不是不同的源。无论哪种方式,您都可以修改下面的代码,但它是针对同一个 iframe 的。

你可以只检测每次更新源时触发的加载事件,并在加载事件函数中递归调用相同的函数,直到到达数组的末尾

$.ajax({
    ...
    success: function(data) {
      loadFrame(data, 0);
    }
 });


function loadFrame(arr, i) {
   //tell it what to do when the frame loads, but just once, since we
   //are incrementing i
   $('#iframe-chart').once('load', function(){
     if (i<arr.length) { loadFrame(arr, i++); }
   });
   var url = "/Reportedit/createonechart"; 
   //you'll need some logic here to figure out which url to load
   $("#iframe-chart").attr("src", url);
 });

【讨论】:

  • 直到我在 Stackoverflow 上获得足够的分数,我才能对此进行投票 LOL 感谢 Dave 抱歉延迟回答...
猜你喜欢
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 1970-01-01
相关资源
最近更新 更多