Search for mysql query in angularjs using ng-keyup but $ http after only 3 seconds

I want to do an online search in angularjs using the ng-keyup function. But I suppose it's wrong to query BD for each keyboard. So, how can I set a timer that will make the $ http service work only if there are no keys in the last 3 seconds?

<input type="text" ng-keyup="search(param.values, param.url)">

JS:

app.controller('searchCtrl', function($scope,$rootScope,$http){
$scope.search = function( values , type) {
    var data={};
    data.values=values;
    data.type=type;
    console.log(data);
    $http.post("search.php", data).then(function success (response) {
            console.log(response.data);
            $rootScope.search_result=response.data;
        },function error (response){
            console.log(response.data);
        }
    );
};
});
+4
source share
2 answers

ng-model-options to debounce , angular ng- . ng-change ng-keyup. , debounce API.

<input type="text" ng-model="value" ng-change="search(param.values, param.url)" 
  ng-model-options="{ debounce: 3000 }"
+2

/.

, , .

, timeout $http.

$http.get(request, {timeout: promise}).then...

:

abort = $q.defer();

, , , , () , .

if(abort) {
  abort.resolve();
}

. ( , .)

+1

Source: https://habr.com/ru/post/1679601/


All Articles