Skip to content

Instantly share code, notes, and snippets.

@makevoid
Created June 11, 2009 01:34
Show Gist options
  • Save makevoid/127643 to your computer and use it in GitHub Desktop.
Save makevoid/127643 to your computer and use it in GitHub Desktop.
Jquery ping
/**
* ping for jQuery
*
* @auth Jessica
* @link http://www.skiyo.cn/demo/jquery.ping/
*
*/
(function($) {
$.fn.ping = function(options) {
var opts = $.extend({}, $.fn.ping.defaults, options);
return this.each(function() {
var ping, requestTime, responseTime ;
var target = $(this);
function ping() {
$.ajax({url: 'empty' + Math.random() + '.html',
type: 'GET',
dataType: 'html',
timeout: 30000,
beforeSend : function() {
requestTime = new Date().getTime();
},
complete : function() {
responseTime = new Date().getTime();
ping = Math.abs(requestTime - responseTime);
target.text(ping + opts.unit);
}
});
}
ping();
opts.interval != 0 && setInterval(ping,opts.interval * 1000);
});
};
$.fn.ping.defaults = {
interval: 3,
unit: 'ms'
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment