【问题标题】:How to set delay timeout for each HTTP get on each button request如何为每个按钮请求的每个 HTTP 设置延迟超时
【发布时间】:2017-05-21 14:57:33
【问题描述】:

我有一个从 database.php 获取数据的按钮。我试图在 http.get 没有响应之前添加超时延迟,所以我不能使用承诺。在 php 中,使用 sleep() 非常简单。至于原因,它是另一个模拟延迟的项目的一部分(我知道你可以通过其他方式做到这一点)。

app.controller('mainController', function ($scope, $http, $timeout) {
     $scope.requestData = function () {
        $scope.delta = '[waiting]';
        //Delay here!
        $http.get('database.php').then(function (response) {
            $scope.rows = response.data.records;
    };
});

我试过了,不行

app.controller('mainController', function ($scope, $http, $timeout) {
     $scope.requestData = function () {
        $scope.delta = '[waiting]';
        $http.get('database.php',{ timeout: 3000 }).then(function (response) {
            $scope.rows = response.data.records;
    };
});

我试过传递一个空的计时器,不起作用

app.controller('mainController', function ($scope, $http, $timeout) {
    var timer = function () {
    }

    $scope.requestData = function () {
        $scope.delta = '[waiting]';
        $timeout(timer, 3000);
        $http.get('database.php').then(function (response) {
            $scope.rows = response.data.records;
    };
});

有什么想法,我能做什么?

【问题讨论】:

    标签: angularjs http timeout delay


    【解决方案1】:

    你需要像这样将你想要延迟的函数传递给超时函数

    app.controller('mainController', function ($scope, $http, $timeout) {
         $scope.requestData = function () {
            $scope.delta = '[waiting]';
            //Delay here!
            $timeout(function () {
                $http.get('database.php').then(function (response) {
                     $scope.rows = response.data.records;
                });
            }, 2000)
        };
    });
    

    【讨论】:

      猜你喜欢
      • 2019-02-13
      • 2018-06-29
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多