Skip to content

Instantly share code, notes, and snippets.

@rhyttr
Created August 10, 2016 10:02
Show Gist options
  • Save rhyttr/e77e581deff7254a735570a96526e252 to your computer and use it in GitHub Desktop.
Save rhyttr/e77e581deff7254a735570a96526e252 to your computer and use it in GitHub Desktop.
angular.ping.js
angular.module('pingService', [])
.factory('ping', ['$http', function($http) {
// Pings a URL or IP using HTTP GET request
return {
ping: function(URL, callback) {
var responseTime = 0;
var start = (new Date()).getTime();
$http.get(URL + '?rnd=' + (new Date().getTime()))
.success(function() {
responseTime = (new Date().getTime()) - start;
callback(Math.round(responseTime / 10) / 100);
})
.error(function() {
callback(responseTime);
})
}
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment