Question: There are lines:

```
echo "1111111111111; 2222222222; 8; 2; 34,12,2,3,5,1; UNKNOW;"

```

or

```
echo "1111111111111; 2222222222; 8; 2; 34,12,2,3,5,1,5,12,45,34; UNKNOW;"

```

or

```
echo "1111111111111; 2222222222; 8; 2; 3; UNKNOW;"

```

How to take the value of the last character ";" and to the last comma ","?

Answer: 
I definitely recommend @jeanpaul's answer for "debouncing".

In addition to that, when you have potential for multiple concurrent AJAX requests and you want to handle the most recent one, it can be necessary to verify which request it is in your response handler. This is especially important when the responses don't always come in the same order they were requested (ie. an earlier request takes longer to respond than a later one)

A way to solve this is something like:

```
var activeRequest;
function doRequest(params){
    // reqId is the id for the request being made in this function call
    var reqId = angular.toJson(params); // I usually md5 hash this

    // activeRequest will always be the last reqId sent out
    activeRequest = reqId; 

    $http.get('/api/something', {data: params})
        .then(function(res){
            if(activeRequest == reqId){
                // this is the response for last request
            }
            else {
                // response from previous request (typically gets ignored)
            }
        });
}

```
You already accepted the answer but I want to share some code with you.

```
myapp.factory('formService', ['$http', '$filter', '$q', '$timeout', function ($http, $filter, $q, $timeout) {
    var service = {};
    service.delayPromise = null;
    service.canceler = null;
    service.processForm = function (url, formData, delay) {
        if (service.delayPromise)
          $timeout.cancel(service.delayPromise);
        if (service.canceler)
          service.canceler.resolve();
        service.canceler = $q.defer();
        service.delayPromise = $timeout(function (service) {
          return service;
        }, delay, true, service);
        return service.delayPromise.then(function (service) {
          service.delayPromise = null;
          return $http({
              method  : 'POST',
              url     : url,
              timeout : service.canceler.promise,
              data    : formData
          });
        })
    }
    return service;
]);

```

What does this do. It provides `formService` having `processForm` function, with accepts `url`, `formData` and `delay`.

`processForm` function delays submitting with `$timeout` service. If there are already exist delayed or pending submission it just cancels it.

And in your controller.

```
myapp.controller('myCtrl', ['formService', function (formService) {
    $scope.formData = {};
    $scope.pageData = {};
    $scope.$watchCollection('formData', function (formData, oldData, scope) {
      if (!angular.equals(formData, oldData)) {
        var event;
        formService.processForm(formData, event, 500).then(function (response) {
          if (response.data instanceof Object)
            angular.copy(response.data, scope.pageData);
        });
      }
    });
}]);

```