首页 > 解决方案 > 如何确定 jQuery 中的行代码已完成

问题描述

我有一个向 API 发送请求以下载 Excel 文件的代码。我想在下载完成时显示一个等待动画停止。我使用此代码下载 excel 文件:

$('#t_ExcelExport')
 .button()
 .click(function () {
    var isApproved = localStorage.getItem('t_Filter_Caption_User_Value');
    let url = PeywebRootPath + 'Security/User/ToExcel?isApproved=' + isApproved;
    $.Waiting();
    window.location = url;
)};

如何确定window.location =已完成的行代码并且我应该停止显示等待动画?

标签: javascriptjquery

解决方案


我找到了解决这种情况的方法:

  1. 完成服务器端下载后。添加一个名为“fileDownloadCompletedToken”的 cookie
  2. 在客户端检查 cookie 是否存在“fileDownloadCompletedToken”
  3. 如果 cookie 存在;停止显示等待动画并删除 cookie 'fileDownloadCompletedToken'

我的完整代码:

$('#t_ExcelExport')
  .button()
    .click(function () {
      var isApproved = localStorage.getItem('t_Filter_Caption_User_Value');
      let url = PeywebRootPath + 'Security/User/ToExcel?isApproved=' + isApproved;
      $.Waiting();
      window.location = url;

      window.setInterval(function () {
      if ($.cookie('fileDownloadCompletedToken') != null)
        $.Waiting({ show: false });
        $.cookie("fileDownloadCompletedToken", null, { path: '/' });
      }, 1000);
    });

推荐阅读