Created
June 11, 2009 01:34
-
-
Save makevoid/127643 to your computer and use it in GitHub Desktop.
Jquery ping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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