$http超时是什么意思?

What is $http timeout mean?

当使用 $http 时,我们可以为其设置超时,它看起来像这样:

$http.get(url,{timeout: 5000}).success(function(data){});

超时是什么意思?是说连接(数据下载)必须在超时时间内完成吗?或者是指从服务器接收响应的延迟时间?移动连接的最佳一般最小超时设置是什么?

timeout – {number|Promise} – 以毫秒为单位的超时,或承诺在解决后应中止请求。 Source

无论如何在 JS 中超时意味着 "perform an action after X time"。

如果 http 请求未在指定的 timeout 时间内完成,则会触发错误。

所以,这有点像对 $http.get() 函数说以下内容:

  1. 我希望你帮我拿这个 URL 并给我数据
  2. 如果你这样做成功了,然后调用我的 .success() 处理程序并给我数据。
  3. 如果请求完成的时间超过 5000 毫秒,则与其继续等待,不如触发超时错误。

仅供参考,在我看来 AngularJS 已转换为使用标准的 promise 语法,因此您可能应该这样做:

$http.get(url,{timeout: 5000}).then(function(data){
    // successfully received the data here
}, function(err) {
    // some sort of error here (could be a timeout error)
});

What is that timeout mean? Is it mean the connection (data download) must be completed within the timeout period?

是的。如果未在该时间段内完成,则会 return 报错。这样可以避免长时间等待请求。

Or it is mean the delay time to receive respond from the server?

不,这不是延迟时间。

What would be the best general minimal timeout setting for mobile connection?

如果没有更多细节,这很难说。许多不同的东西可能会驱动您将其设置为什么。有时,让超时设置为一个相当长的值(例如 120 秒)并没有什么坏处,以防某天服务器或某些移动设备 link 碰巧特别慢并且您想给它尽可能多的机会在这种情况下取​​得成功。在其他情况下(取决于特定的用户交互),如果响应时间超过 5 秒,用户无论如何都会放弃,因此等待比用户已经放弃的结果更长的时间可能没有任何价值。