当用户停止输入 angularjs 时,有没有办法在两 2
秒后向服务器发送请求。例如 debuuncing 。换句话说,如果用户输入“abc”然后停止等待两秒然后点击服务器。
目前,每当我输入它向服务器请求的任何字符时 这是我的代码 http://plnkr.co/edit/npiA2abAo5SEQFMMpKZO?p=preview
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope,$http) {
$scope.name = 'World';
$scope.keyupevt = function(){
console.log('xx')
$http.get("data.json")
.then(function(response) {
console.log(response)
});
}
});
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<input type="text" ng-keyup="keyupevt()" />
</body>
最佳答案
使用setTimeout函数
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope,$http) {
$scope.name = 'World';
$scope.keyupevt = function(){
setTimeout(function(){
$http.get("data.json").then(function(response)
{
console.log(response)
}
)}, 2000);//delay parameter of 2 seconds
}
});
关于javascript - 如何在 AngularJS 中的 keyup 事件上两秒后发送请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53140914/