我正在使用 Nodejs 从服务器请求数据,但我不知道如何处理同一请求中延迟的响应数据,这是前面的代码:
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>
角:
function testCtrl($scope, $http) {
var config;
$scope.animalSpecies = {
species: ""
}
$scope.selectAnimal = function () {
config = {
method: "GET",
url: "/getanimal",
params: {
species: $scope.animalSpecies.species
}
}
$http(config).then(
function (res) {
console.log(res.data);
}
)
}
}
Nodejs
app.get("/getanimal", function (req, res) {
var species = req.query.species;
if (species === "Dog") {
setTimeout(function () {
res.send({msg: "species is" + species});
}, 1000)
} else {
res.send({msg: "species is" + species});
}
})
当您先单击"狗"选项并快速单击"猫"选项时,"猫"的响应数据会先出来,最后"狗"的数据将在一秒钟后响应。(我在 Nodejs 中做了一个设置超时延迟,但也许其他条件,如查询数据库回调延迟或以某种方式)
我想要的只是:有没有办法停止服务器响应数据或我在前端得到了我想做的事情吗?
最好的方法是在第一个响应尚未返回时禁用用户的控件。
当用户单击其中一个输入按钮时禁用输入按钮,并在服务器响应时启用它们