0

For my website I'm making an http request for accessing an API. It triggers on key press event. The problem is it makes many ajax calls on each continuous key press, so for controlling this I have to add a small timeout like a 300 ms. I have made http call as follows, how can I modify this now?

var req = {
    method: 'GET',
    url: 'url of api',
    headers: {
        "Key": "keyofapi",
        "Accept": "application/json"
    },
};

$http(req)
    .success(function(data, status, headers, config) {
    })
    .error(function(data, status, headers, config) {
    });
5
  • What exactly are you asking? Your question is unclear. Commented Jan 22, 2015 at 6:56
  • @Jan I want to add a delay while making requests , check this link s6.postimg.org/bw2uuera9/svo.png Commented Jan 22, 2015 at 6:58
  • you can disable the function if is running, Commented Jan 22, 2015 at 7:44
  • @SebastiánEspinosa I didn't get , what you mean, can you describe please ? Commented Jan 22, 2015 at 8:12
  • i mean something like a flag, when the function is running set the flag to false, when is ready to run again set it to true, and then you can use that flag on a if where the function is triggered, so the function only runs when its not running.. Commented Jan 22, 2015 at 9:32

3 Answers 3

1

Wrap your function inside $timeout function, mention the timeout (P.S it takes value in ms). Dont forget to inject $timeout in your controller parameters. Try this,

$timeout(function () {
        $http({
                method: 'POST', //No I18N
                url: 'your URL', //No I18N
                params: { 
                    parameters if needed
                }
            }).success(function (data, status, headers, config) {
                //Do the required
            });
        }, 2500);
Sign up to request clarification or add additional context in comments.

Comments

1

I actually recommend to do a factory or service that watching if you have active request and you waiting for it.

yourApp.factory('apiReqeust', ['$http', function($http) {
  // If you want to complete all requests that you sending,
  // make this varable an empty array and push every new request inside it.
  var waitingRequest = null; // Or quenuedRequests = [];
  var isWorking = false;
  // In my case I will not make an array, and will have only one request that will wait.
  // I mean the last attempted request.
  // This is perfect for an Autocomplete API

  function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});

The usage of this is:

// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);

I am not tested this code. I don't know if it's working. But I hope you got the idea.

1 Comment

See my edit in comments. I forgot the return statement at line 17.
1

I actually recommend to do a factory or service that watching if you have active request and you waiting for it.

 function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});

The usage of this is:

// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.