我正在使用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});
}
})
当您点击&#34; Dog&#34;选项首先快速点击&#34; Cat&#34;选项, &#34; Cat&#34;的响应数据将首先出现,但最终是&#34; Dog&#34;&#34; s 数据将在一秒后响应。(我在Nodejs中执行setTimeout延迟,但也许 其他条件,如查询数据库回调延迟或某种方式)
我想要的只是:是否有某种方法可以阻止服务器响应数据或 我得到了我想在前端做的事情?
答案 0 :(得分:0)
如果第一个响应还没有回来,最好的方法是禁用用户控件。
当用户单击其中一个时禁用输入按钮,并在服务器响应中启用它们