Javascript 如何防止iframe两次运行load事件?

Javascript 如何防止iframe两次运行load事件?,javascript,jquery,html,iframe,Javascript,Jquery,Html,Iframe,我在谷歌上搜索了这个问题,并尝试了许多片段;但是什么都没有。如何防止iframe两次运行加载事件 PAGE.HTML:正在运行 如您所见,我已经尝试了任何可能的方法来防止两次运行ajax请求。但我仍然可以在控制台中看到哪些ajax调用是两次并行进行的。我的意思是,我同时收到重复的请求…消息等。请问有什么建议吗?提前感谢。如果您只想执行一次请求,可以使用全局标志var来防止多次加载,而不是超时: var has_requested = false; //The global flag f

我在谷歌上搜索了这个问题,并尝试了许多片段;但是什么都没有。如何防止
iframe
两次运行加载事件

PAGE.HTML:正在运行


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

如果您只想执行一次请求,可以使用全局标志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); 
});

你的代码片段似乎太复杂了,为什么要在超时时调用请求?因为我想在超时时重复调用好的,我错过了你在超时回调中调用它的部分:
doRequests(startRequests)我越看你的问题,我就越不确定你想要实现什么……我理解你的意思正确吗?@Bigood我正在尝试循环ajax请求。但代码似乎正在循环。在两个加载事件中,
已请求的
将为
。似乎该变量有两个实例!我代码中的
requestsTimeout
也会这样做。但他们(我的或你的)都没有帮忙。谢谢你的努力。Cheers@Javad_Amiry你不是在加载你的脚本两次吗?我仔细检查了所有我能想到的东西,包括你提到的那一点。我完全糊涂了。为了进一步帮助,我最终需要访问您的开发站点…
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();
});
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();
});
$(window).load(function () {
    //You'd better store the interval identifier to clear it afterwhile, 
    //if you think you'll need to.
    setInterval(doRequests,5000); 
});