AngularJS: Send $http.get requests continuously until certain condition

As the title says, I want to send $http.get requests to the server until the returned results meets a condition.

This is what I tried:

var searchComplete = false; var sendGetReq = true; while(!searchComplete) // #2 and then jumps here { if(sendGetReq) { sendGetReq = false; // #1 execution reaches here $http.get('/user/getCondition', {params: {condition: $scope.condition}). success(function (condition, status, headers, config) { ... ... if(condition) { searchComplete = true; } else { sendGetReq = true; } } }

However, the $http.get request never goes. That statement is never reached. When I debugged, I saw that execution will come to sendGetReq=false and then jump to while(!searchComplete). The GET request is never sent.

Would someone please explain:

<ol> <li>Why the execution never reaches GET request and jumps back</li> <li>What is the recommended way to to do this continuous GET request</li> </ol>

Thank you.


Why not put it all together inside a function and call itself if condition is meet? Like this and you should use .then and not .success:

$scope.load = function(){ $http.get('/user/getCondition', {params: {condition: $scope.condition}). then(function (condition, status, headers, config) { if (condition){ $scope.load(); } }); }

Hope it helps =)


Do not use while (true), especially in JS.

JS works in one thread. $http.get will never run because your main thread stalled in you infinite loop and do not gave a chance for other threads to run.


function getHttpPromise() { return $http.get('/user/getCondition', {params: {condition: $scope.condition}). success(function (condition, status, headers, config) { ... ... if(condition) return true;//or any result you want to resolve else return getHttpPromise(); } }

getHttpPromise function returns promise, which will be resolved on your condition, in other case it will continues waiting with followed get request.

To use it, run the following code.

getHttpPromise().then(function (result) { // Will be called only when condition will be successful });


$http.get is asynchronous call and it will hit the success or error (both are deprecated, use then) function on response from server. When the function reaches this line

sendGetReq = false; // #1 execution reaches here

it makes the $http.get call (which is async) and then it moves the control to next iteration. That is the main purpose of async call.

If you want to make continuous calls you can write the $http.get call in a function and call that function using interval service of Angular JS.


$scope.func = function(){ if(condition) { $http.get.... } } $interval($scope.func, 1000)

where 1000 is the time interval in milliseconds after which the function is called


Try this inject $interval in your controller

var startInterval = $interval(function(){ var searchComplete = false; var sendGetReq = true; sendGetReq = false; // #1 execution reaches here $http.get('/user/getCondition', {params: {condition: $scope.condition}). success(function (condition, status, headers, config) { if(condition) { searchComplete = true; $scope.stopInterval(); } else { sendGetReq = true; } }) },1000) $scope.stopInterval = function(){ $interval.cancel(startInterval) }


As Andrew said it should be recursive and in your case use .then. To make it recursive encapsulate your $http service inside a function

.controller('YourController', function($http) { this.sendGetReq= function(data) { $http.get('/user/getCondition', {params: {condition: data.condition}). .then(function(res) { if (res.condition) { //finish your search } else { //set newdata somehow this.sendGetReq(newdata); } } };



  • Drop variable in panel data in R conditional based on a defined number of consecutive observations
  • When is InputStream.available() useful?
  • Dropping support for JRE 1.3
  • Domain name setup with AWS S3 bucket with static hosting
  • Get MP3 ID3 meta data and song duration using AudioStreamer
  • Should a C# struct have only read-only properties
  • Whats the best way of persisting data to Isolated Storage on Windows Phone 7?
  • What version of Java should I use with Cassandra 2.0?
  • Error in GGally: Error in unit(tic_pos.c, “mm”) : 'x' and 'units' must have leng
  • How to wait for all async tasks to finish in Node.js?
  • Zend framework 2 : Add different authentication adapter for two different modules
  • PHP get selected value of select form
  • Mvc 3 posting content type application/json, action method arguments loose mapping?
  • Is it better to use the “hidden” CSS attribute or fetch each set of new images?
  • retrieving data from url in iphone
  • How to apply a custom handlers to only specific folder
  • Tap to record like in vine using javacv
  • Google Maps api v3 get start and end coordinates of a street
  • HttpClient: disabling chunked encoding
  • How to 'create temp table as select' in Slick?
  • How to get latest version of a artifact on Bintray using JSONP
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • MySQL Order by column = x, column asc?
  • How can I speed up CURL tasks?
  • Bad request using file_get_contents for PUT request in PHP
  • Java: can you cast Class into a specific interface?
  • Seeking advice on Jetty HttpClient Hang
  • AES padding and writing the ciphertext to a disk file
  • How would I use PHP exceptions to define a redirect?
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Updating server-side rendering client-side
  • How to extract text from Word files using C#?
  • Symfony2: How to get request parameter
  • Weird JavaScript statement, what does it mean?
  • log4net write single file for each call to log.info
  • Getting error when using KSoap library to consume .NET web services
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Busy indicator not showing up in wpf window [duplicate]
  • Why do underscore prefixed variables exist?