【问题标题】:How to prevent an iframe from running load event twice?如何防止 iframe 运行两次加载事件?
【发布时间】:2014-12-10 14:13:40
【问题描述】:

我用谷歌搜索了这个问题,并尝试了很多 sn-ps;但一无所获。如何防止iframe 运行两次加载事件?

PAGE.HTML:http://maindomain.com运行

<div class="regionBody">
    <iframe frameborder="0" allowtransparency="true" src="http://externaldomain.com/iframepage" height="215" style="border:none;width:100%;"></iframe>
</div>

<script>
$('.regionBody iframe').css({height: someCalculatedHeight});
</script>

IFRAMEPAGE.HTML:http://externaldomain.com运行

<div>some elements...</div>
<script src="frame-script.js"/>

frame-script.js:

var requestsTimeout = null;

function doRequests(callback) {
    clearTimeout(requestsTimeout);
    $.ajax({
        cache: false,
        url: some-url,
        dataType: 'json',
        type: 'GET'
    }).done(function (data) {
        // putting the result in frame's body
    }).complete(function () {
        clearTimeout(requestsTimeout);
        callback();
    });
}

function startRequests() {
    requestsTimeout = setTimeout(function () {
        console.log('Doing requests...');
        doRequests(startRequests);
    }, 5000);
}

$(window).load(function () {
    console.log('IFrame loaded...');
    $(window).resize(function () {
        clearTimeout(requestsTimeout);
    });
    $(this).resize(function () {
        clearTimeout(requestsTimeout);
    });
    $('body').resize(function () {
        clearTimeout(requestsTimeout);
    });
    startRequests();
});

如您所见,我已经尝试了任何可能的方法来防止两次运行 ajax 请求。但我仍然可以在控制台中看到哪些 ajax 调用并行进行了两次。我的意思是我同时收到重复的Doing requests... 消息,等等。请问有什么建议吗?提前致谢。

【问题讨论】:

  • 你的sn-p好像太复杂了,为什么要超时调用request?
  • 因为我想在一段时间内重复通话
  • 哈好吧,我错过了你在超时回调中回忆的部分:doRequests(startRequests);
  • 我越看你的问题,就越不确定你想要达到什么目的……我理解正确吗?
  • @Bigood 我正在尝试创建一个 ajax 请求周期。但似乎代码正在循环。

标签: javascript jquery html iframe


【解决方案1】:

如果您只想执行一次请求,可以使用全局标志 var 来防止多次加载而不是超时:

var has_requested = false;     //The global flag

function doRequests(callback) {
    //Quit if there's a request in progress
    if(has_requested)
        return;

    has_requested = true;

    $.ajax({
        cache: false,
        url: some-url,
        dataType: 'json',
        type: 'GET'
    }).done(function (data) {
        // putting the result in frame's body
    }).complete(function () {
        callback();
    });
}

$(window).load(function () {
    //Don't forget to pass your callback here, I let it blank
    doRequests();
});

如果您只是想避免并行请求,您可以在 .done().complete() 中将标志重置为 false,这样它只会在第一个请求实现时才会请求。

编辑

如果您想随着时间的推移重复,请使用setInterval

$(window).load(function () {
    //You'd better store the interval identifier to clear it afterwhile, 
    //if you think you'll need to.
    setInterval(doRequests,5000); 
});

【讨论】:

  • has_requested 在两个加载事件中都是 false。似乎有该变量的两个实例!我的代码中的requestsTimeout 也会这样做。但是他们(我的或你的)都没有帮助。但感谢尝试。干杯
  • @Javad_Amiry 你不是两次加载你的脚本吗?你应该仔细检查一下。
  • Nop ):我仔细检查了我能想到的所有内容,包括您提到的这一点。我完全糊涂了。
  • 为了进一步提供帮助,我最终需要访问您的开发站点...
猜你喜欢
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多