【问题标题】:jQuery .ajax or .post taking too long message [duplicate]jQuery .ajax 或 .post 消息太长[重复]
【发布时间】:2014-05-16 01:43:02
【问题描述】:

我知道 ajax 调用的超时设置。但我想知道的是,如果 ajax 调用仍在处理但时间超过 x 秒,是否有办法向用户显示消息。

例如

在 ajax 调用期间,如果需要超过 10 秒的时间告诉用户,“调用时间比预期的要长”

【问题讨论】:

标签: javascript jquery


【解决方案1】:

我想说你最好的选择是在显示通知之前使用window.setTimeout 等待多久,然后在你的$.ajax() 调用中添加一个window.clearTimeout 行到你的success 回调:

var loadingTimeout = window.setTimeout(function() {
  // show your warning here
  alert('Still loading :P');
}, 10000); // 10000ms = 10sec

$.ajax({
  url: 'http://your/url/here',
  dataType: 'json',
  type: 'GET',
  success: function(r) {
    window.clearTimeout(loadingTimeout);
    // your results logic here
  }
})

【讨论】:

  • 如果 ajax 调用失败,将 clearTimeout 行放在完整的回调中怎么样?
【解决方案2】:

当然,只需 setTimeout() 自己使用另一个函数来检查由 ajax 完成回调设置的一些全局变量。在该函数中,如果 ajax 调用仍未完成,则显示一条消息。

【讨论】:

    猜你喜欢
    • 2012-01-23
    • 2013-09-04
    • 2011-08-03
    • 1970-01-01
    • 2012-01-22
    • 2013-03-08
    • 2012-06-05
    • 2013-05-02
    • 2022-06-15
    相关资源
    最近更新 更多