简体   繁体   English

jQuery在页面卸载时打破每个循环

[英]jQuery break an each loop on page unload

I have a jQuery each loop with an ajax function inside but I'm experiencing an issue when trying to navigate away from this page while the loop is still running, basically I can't navigate to any other page unless the each loop has finished. 我有一个jQuery每个循环都有一个ajax函数,但是当我在循环仍在运行时试图离开这个页面时我遇到了一个问题,基本上我无法导航到任何其他页面,除非每个循环都已完成。

I don't need this loop to finish because it's basically only needed as a visual indicator, its far from being so important to sacrifice user experience for. 我不需要这个循环来完成,因为它基本上只需要作为一个视觉指示器,它远没有牺牲用户体验那么重要。

    $.each( get_link, function( i, val ) {

    $(window).on("beforeunload", function() {
        return false;
    });

    $.ajax({
        type: "POST",
        url: url,
        async: true,
        data: {url: val},
        success: function(data)
        {
            let row = $("#status[data-url='" + val + "']");
            row.html(data);
            $("#loader[data-url='" + val + "']").removeClass("loader vertical-align-middle loader-bounce");
        }
    });
});

As you can see I have tried to fix it with $(window).on("beforeunload") but beforeunload creates an unnecessary alert window which unfortunately I couldn't find a way to disable. 你可以看到我试图用$(window).on("beforeunload")修复它,但beforeunload创建了一个不必要的警报窗口,遗憾的是我找不到禁用的方法。

EDIT: The above code isn't breaking the loop either... 编辑:上面的代码也没有破坏循环...

Is beforeunload a good idea? beforeunload一个好主意? Maybe there are better approaches to break a loop on page exit? 也许有更好的方法来打破页面退出循环?

f.khantsis suggested to abort the ajax operation which is a good idea but it doesn't help because I run an each loop on an array with 5-6 keys. f.khantsis建议中止ajax操作,这是一个好主意,但它没有帮助,因为我在5-6个键的数组上运行每个循环。 So if one of them is aborted the loop just goes on to run with all the remaining keys. 因此,如果其中一个被中止,则循环继续运行所有剩余的键。

see Abort Ajax requests using jQuery 请参阅使用jQuery中止Ajax请求

Basically, jQuery.ajax returns a jqXHR object, which can be used to abort a pending request. 基本上, jQuery.ajax返回一个jqXHR对象,该对象可用于中止挂起的请求。

$.each( get_link, function( i, val ) {

    var xhr = $.ajax({
      type: "POST",
      url: url,
      async: true,
      data: {url: val},
      success: function(data)
      {
          let row = $("#status[data-url='" + val + "']");
          row.html(data);
          $("#loader[data-url='" + val + "']").removeClass("loader vertical-align-middle loader-bounce");
      }
    });
    $(window).on("beforeunload", function() {
      xhr.abort();
    });
});

$(window).on("beforeunload") should probably be outside your loop. $(window).on("beforeunload")应该在你的循环之外。

Then it can set a global variable which you would then check for in your loop, something like: 然后它可以设置一个全局变量,然后在循环中检查它,例如:

var leavingThePage = false;

$(window).on("beforeunload", function() {
        leavingThePage = true;
});

$.each( get_link, function( i, val ) {
    if (leavingThePage)
        return false;

    $.ajax({
      ... Do ajax stuff ...
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM