加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 延迟一个angular.js $http服务

发布时间:2020-12-17 08:35:46 所属栏目:安全 来源:网络整理
导读:我有一些有角度的工厂,用于对传统的ASP.NET .asmx Web服务进行Ajax调用,如下所示: module.factory('productService',["$http",function ($http) { return { getSpecialProducts: function (data) { return $http.post('/ajax/Products.asmx/GetSpecialPro
我有一些有角度的工厂,用于对传统的ASP.NET .asmx Web服务进行Ajax调用,如下所示:
module.factory('productService',["$http",function ($http) {
    return {
        getSpecialProducts: function (data) {
            return $http.post('/ajax/Products.asmx/GetSpecialProducs',data);
        }
    }
} ]);

我在本地网络测试,所以响应时间是“太”好。有一个聪明的方式延迟$ http几秒钟从呼叫模拟一个坏的连接?

或者我需要在$ timeout中包装所有调用的工厂方法?

$timeout(function() {
  productService.getSpecialProducs(data).success(success).error(error);
},$scope.MOCK_ajaxDelay);
有趣的问题!

正如你自己提到的,$ timeout是延迟调用的最合理的选择。而不是在每个地方都有$ timeout调用,你可以推送一个响应拦截器将$ http promise包装在一个$ timeout promise中,就像在documentation of $http中概念概述的那样,然后在一个配置块中注册它。这意味着所有$ http调用都受到$ timeout延迟的影响。类似的东西:

$httpProvider.interceptors.push(function($timeout) {
    return {
        "response": function (response) {
            return $timeout(function() {
                return response;
            },2500);
        }
    };
});

作为你的“模拟一个坏的连接?”的奖励,你可以拒绝或绝对没有任何随机。嘿嘿。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读