如何为每个按钮请求的每个 HTTP 设置延迟超时

How to set delay timeout for each HTTP get on each button request

我有一个从 database.php 获取数据的按钮。我正在尝试在 http.get 未响应之前添加超时延迟,因此我无法使用 promise。在 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;
    };
});

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

你需要像这样把你要延迟的函数传入timeout函数

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)
    };
});