很多網站現在都會展示實時的在線人數,吾愛編程接下來就分享一下使用PHP顯示實時網站在線人數
使用的ajax長輪詢 ajax long poll,為了便於使用,我將它封裝成了一個jquery插件
(function($){
$.fn.extend({
visitors: function(options){
var defaults = {
script : "visitors/online.php",
timeout : 50000,
};
var options = $.extend(defaults, options);
return this.each(function(){
var holder = $(this);
refresh = function(sleep) {
var sleep = sleep ? sleep : 1000;
setTimeout('update()', sleep);
}
update = function() {
$.ajax({
url : options.script,
type : 'get',
dataType : 'json',
async : true,
cache : false,
timeout : options.timeout,
beforeSend : function() {
},
success : function(data) {
holder.html(data.num);
refresh(1000);
},
error : function() {
refresh(15000);
}
});
}
update();
});
}
});
})(jQuery);
*