/**
* @author 全冠清
*/
(function($){
var methods = {
init: function(opts){
if(!$(window).data('register_messageline')){
$(window).data('register_messageline',true)
}else{
alert('error:只允许有一个请求总线')
return;
}
var defaults = {
actions:[],
timeout: 80000//默认ajax请求超时时间80秒
}
var setting = $.extend(true, defaults, opts)
if(!$(window).data('messageline')){
$(window).data('messageline',[])
}
$.each(setting.actions,function(i,item){
$(window).data('messageline').push(item)
})
function messageline(){
$.ajax({
type: "GET",
dataType: "json",
url: setting.url,
timeout: setting.timeout,
data:setting.data,
success: function(messages, textStatus){
//从服务器得到数据,显示数据并继续查询
$.each(messages,function(i,item){
$.each($(window).data('messageline'),function(j,active){
if(active.name===item.name){
active.action(item.data)
}
})
})
messageline()
},
//Ajax请求超时,继续查询
error: function(XMLHttpRequest, textStatus, errorThrown){
if (textStatus == "timeout") {
messageline()
}
}
});
}
messageline()
},
addlistener: function(active){
if(!$(window).data('messageline')){
$(window).data('messageline',[])
}
$(window).data('messageline').push(active)
}
};
$.comet = function(method){
// Method calling logic
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
else
if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
}
else {
$.error('Method ' + method + ' does not exist on jQuery.comet');
}
};
})(jQuery);
用法
//初始化(仅需要一次)
$.comet({
url:'url'
})
//使用(可任意多种消息类型)
$.comet('addlistener',{name:'newMessage(请求的名字)',action:function(data){
//$('.new_state').html('有'+data+'条新动态,赶快点击查看哦!').show()
}})
分享到:
相关推荐
、CSS和JavaScript知识的开发者,内容覆盖了jQuery知识体系的全部内容,包括jQuery Core、jQuery Plugin 、jQuery UI、jQuery Mobile以及大量第三方的插件库和2800多个应用jQuery技术的网页参考。
jquery 的经典 英文书 jQuery Plugin Development Beginner’s Guide.PDF
表格拖拽排序插件 Table Drag and Drop JQuery plugin v0.7 最新0.7版本
jQuery plugin for autocomplete - jQuery自动完成插件
struts2-jquery-plugin-3.1.0.jar
promptuMenu - jQuery Plugin
jQuery Media Plugin Demo
aptana jquery plugin js javascript
jQuery Plugin Prin YourPage
* usage as: * var slider = $.fn.jSlider({...}); * slider.setSliderValue(value,callback);
JQuery Cycle Plugin
struts2-jquery-plugin-2.0.0 struts2-jquery-plugin-2.0.0 struts2-jquery-plugin-2.0.0
JQuery validation plugin学习资料
jQuery Orbit Plugin 1.2.3
struts2 jquery plugin ,学习struts2 ,又希望在其中使用Jquery的朋友,不妨看看
Auto-Save your content when editing in HTML input and textarea DOM element. http://geniuscarrier.com/autosave-a-jquery-plugin/
flexigrid..... jquery grid plugin
One click to scroll back to the top of the webpage. http://geniuscarrier.com/scrolltotop-a-jquery-plugin/
最新的struts2-jquery-plugin插件3.3.3,包括jquery-ui和grid,使用标签实现。
Auto-Save your content when editing in HTML input and textarea DOM element. http://geniuscarrier.com/autosave-a-jquery-plugin/