javascript - 如何在 AngularJS 中的 keyup 事件上两秒后发送请求?

标签 javascript angularjs

当用户停止输入 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/

相关文章:

javascript - jQuery Dim 或在悬停时更改其他列表项

javascript - 为什么我的 firebase 云函数进程从未完成?

angularjs - 从 Controller 重定向到ons-template

css - Angular Material 在中间制作水平线的方式

javascript - 找不到定义的函数

javascript - 当阅读缩小的 Javascript 时,我应该如何阅读带有逗号、&& 和 || 的返回语句运营商?

javascript - Bootstrap 如何知道元素是否有弹出窗口?

javascript - 结合 Angular 和 React 来简化

javascript - 在应用程序代码上运行 Google Closure Compiler 后更新 AngularJS 模板 HTML

javascript - AngularJS 重定向到另一个 ng-app 模块