Skip to content

Instantly share code, notes, and snippets.

@vfulco
Created January 5, 2016 14:12
Show Gist options
  • Save vfulco/947e688aa69ad26363c4 to your computer and use it in GitHub Desktop.
Save vfulco/947e688aa69ad26363c4 to your computer and use it in GitHub Desktop.
(function () {
'use strict';
angular.module('WordcountApp', [])
.controller('WordcountController', ['$scope', '$log', '$http', '$timeout',
function($scope, $log, $http, $timeout) {
$scope.getResults = function() {
$log.log("test");
// get the URL from the input
var userInput = $scope.input_url;
// fire the API request
$http.post('/start', {"url": userInput}).
success(function(results) {
$log.log(results);
getWordCount(results);
}).
error(function(error) {
$log.log(error);
});
};
function getWordCount(jobID) {
var timeout = "";
var poller = function() {
// fire another request
$http.get('/results/'+jobID).
success(function(data, status, headers, config) {
if(status === 202) {
$log.log(data, status);
} else if (status === 200){
$log.log(data);
$scope.wordcounts = data;
$timeout.cancel(timeout);
return false;
}
// continue to call the poller() function every 2 seconds
// until the timeout is cancelled
timeout = $timeout(poller, 2000);
});
};
poller();
}
}
]);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment