global.js
来自「ajax+visalsdio2005实现的聊天室程序」· JavaScript 代码 · 共 120 行
JS
120 行
ChatRoom = new function()
{
this._serviceUrl = "ChatService.ashx";
this._getUserListInterval = 5000; // 5 seconds
this._getMessagesInterval = 1000; // 2 seconds
this._updateInterval = 300000; // 5 minutes
this._createDelegate = function(instance, method)
{
return function()
{
method.apply(instance, arguments);
}
}
this._ajaxUpdate = function(params, elementId, callback)
{
new Ajax.Updater(
elementId,
this._serviceUrl,
{
method: "post",
evalScript: true,
parameters: params,
onFailure: callback,
onException: callback,
onComplete: callback
});
}
this._ajaxRequest = function(params, callback)
{
new Ajax.Request(
this._serviceUrl,
{
method: 'post',
evalScript: true,
parameters: params,
onFailure: callback,
onException: callback,
onComplete: callback
});
}
this.getUserList = function()
{
this._ajaxUpdate(
"action=users",
"UserList",
this._createDelegate(this, this._getUserListCallback));
}
this._getUserListCallback = function()
{
setTimeout(
this._createDelegate(this, this.getUserList),
this._getUserListInterval);
}
this.getMessages = function()
{
this._ajaxUpdate(
"action=get",
"ChatContent",
this._createDelegate(this, this._getMessagesCallback));
}
this._getMessagesCallback = function()
{
var ele = $("ChatContent");
ele.scrollTop = ele.scrollHeight;
setTimeout(
this._createDelegate(this, this.getMessages),
this._getMessagesInterval);
}
this.update = function()
{
this._ajaxRequest(
"action=update",
this._createDelegate(this, this._updateCallback));
}
this._updateCallback = function()
{
setTimeout(
this._createDelegate(this, this.update),
this._updateInterval);
}
this.sendMessage = function()
{
var message = $("UserInputTextBox").value;
if (message)
{
$("UserInputTextBox").value = "";
var encodedMsg = encodeURI(message);
this._ajaxRequest(
"action=send&msg=" + encodedMsg);
}
}
this.inputBox_KeyPress = function(e)
{
if (e.keyCode == 13)
{
this.sendMessage();
try
{
e.preventDefault();
}
catch(e){}
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?