相同请求异步问题
发布于 8 年前 作者 suyuanhan 2874 次浏览 来自 问答

nodejs

app.get("/getanimal", function (req, res) {
        var species = req.query.species;
        if (species === "Dog") {
            setTimeout(function () {
                res.send({msg: "species is" + species});
            }, 2000)
        } else {
            res.send({msg: "species is" + species});
        }
    })

html:

<div ng-controller="testCtrl">
<label>
    <input type="radio" ng-model="animalSpecies.species" ng-click="selectAnimal()" name="animal" value="Dog"/>Dog
</label>
<label>
    <input type="radio" ng-model="animalSpecies.species" ng-click="selectAnimal()" name="animal" value="Cat"/>Cat
</label>

angular:

function testCtrl($scope, $http) {
        var config;
        $scope.animalSpecies = {
            species: ""
        }
        $scope.selectAnimal = function () {
            config = {
                method: "GET",
                url: "/getanimal",
                params: {
                    species: $scope.animalSpecies.species
                }
            }
            console.log(config);
            $http(config).then(
                    function (res) {
                        console.log(res.data);
                    }
            )

        }
    }

当我点击Dog并且快速点击Cat的时候,cat的获取数据会第一次获得,但是Dog会是最后获取的. 问题是我不希望获得Dog的数据,有什么方法去停止Dog的数据的反馈?

回到顶部