Goku,
estou anexando a esse post uma classe em javascript que fiz para tentar ajudar no seu problema,
vou postar o código aqui e comentar.
aquivo chat.js
[codebox] var Chat = function(params){
//atributos
var chatIntervalID;
var userID;
var objVLD1;
var objVLD2;
var objVLD3;
var objVLD4;
var delay;
var count_req = 0; //se quiser pode tirar esse atributo
//construtor
this.constructor = function(){
//esse if's não para garantir que os atributos sera colocados como parametro
//caso algum valor não seja atribuido, sera lançada uma
if(typeof(params.delay)!='number')
throw 'delay tem que ser number';
if(typeof(params.userID)!='string')
throw 'userID tem que ser string';
if(typeof(params.objVLD1)!='string')
throw 'objVLD1 tem que ser string';
if(typeof(params.objVLD2)!='string')
throw 'objVLD2 tem que ser string';
if(typeof(params.objVLD3)!='string')
throw 'objVLD3 tem que ser string';
if(typeof(params.objVLD4)!='string')
throw 'objVLD4 tem que ser string';
//caso tudo ocorra bem, as atributos serão setados
delay = params.delay;
userID = params.userID;
objVLD1 = params.objVLD1;
objVLD2 = params.objVLD2;
objVLD3 = params.objVLD3;
objVLD4 = params.objVLD4;
}
//métodos
//inicia o setInterval
this.startChat = function(){
if(typeof(chatIntervalID)=='undefined')
chatIntervalID = window.setInterval(
function(){
//seu code vai aqui, esse é um code de exemplo que conta as iterações do setInterval
document.title = 'requisições http : '+(++count_req);
},
delay
);
}
//mata o setInterval
this.stopChat = function(){
if(typeof(chatIntervalID)=='number'){
window.clearInterval(chatIntervalID);
chatIntervalID = undefined;
}
}
//métodos de acesso
this.getUserID = function(){
return userID;
}
this.getObjVLD1 = function(){
return objVLD1;
}
this.getObjVLD2 = function(){
return objVLD2;
}
this.getObjVLD3 = function(){
return objVLD1;
}
this.getObjVLD4 = function(){
return objVLD4;
}
this.getDelay = function(){
return delay;
}
//retorna se o setInterval esta rodando(true) ou não(false)
this.getStatus = function(){
if(typeof(chatIntervalID)=='undefined')
return false;
else
return true;
}
//métodos modificadores
this.setUserID = function(value){
if(typeof(value)!='string')
throw 'userID tem que ser string';
userID = value;
return this;
}
this.setObjVLD1 = function(value){
if(typeof(value)!='string')
throw 'objVLD1 tem que ser string';
objVLD1 = value;
return this;
}
this.setObjVLD2 = function(value){
if(typeof(value)!='string')
throw 'objVLD2 tem que ser string';
objVLD2 = value;
return this;
}
this.setObjVLD3 = function(value){
if(typeof(value)!='string')
throw 'objVLD3 tem que ser string';
objVLD3 = value;
return this;
}
this.setObjVLD4 = function(value){
if(typeof(value)!='string')
throw 'objVLD4 tem que ser string';
objVLD4 = value;
return this;
}
//altera o tempo de delay, caso o chat esteja rodando, ela vai parar o chat atribuir o novo delay e iniciar novamente.
this.setDelay = function(value){
if(typeof(value)!='number')
throw 'delay tem que ser number';
if(this.getStatus()){
this.stopChat();
delay = value;
this.startChat();
}
else{
delay = value;
}
return this;
}
//chamada método construtor
this.constructor();
}
[/codebox]
aquivo html
[codebox]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "
http://www.w3.org/TR...l1-strict.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" xml:lang="pt-BR" lang="pt-BR">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title> </title>
<script type="text/javascript" src="chat.js"></script>
<script type="text/javascript">
var myChat; //deixe a variavel myChat fora do onload para ser acessivel a qualquer js
window.onload = function(){
//criando instancia da class Chat
myChat= new Chat({
userID:'123AFK',
objVLD1:'teste1',
objVLD2:'teste2',
objVLD3:'teste3',
objVLD4:'teste4',
delay: 2000
});
//iniciando chat, alterar o códgio dentro do setInterval
myChat.startChat();
//código de teste para trabalhar com a classe
window.setTimeout(function(){
alert('vou para o chat por 10 seg, chat status: '+myChat.getStatus());
myChat.stopChat();
alert('chat status: '+myChat.getStatus());
},10000);
window.setTimeout(function(){
alert('voltando com o chat');
myChat.startChat();
},20000);
window.setTimeout(function(){
alert('acessando propriedade delay: '+myChat.getDelay());
myChat.setDelay(4000);
alert('alterado tempo de atualização para 4 segundos');
alert('acessando propriedade delay: '+myChat.getDelay());
},20000);
//os metodos set tem como retorno o proprio objeto(this) então, para esses métodos a da para fazer assim:
alert('acessando propriedade delay: '+myChat.getDelay());
alert('acessando propriedade userID: '+myChat.getUserID());
myChat.setDelay(6000).setUserID('bobesponja1122');
alert('alterado userID e delay');
alert('acessando propriedade delay: '+myChat.getDelay());
alert('acessando propriedade userID: '+myChat.getUserID());
}
//exemplo tratamento de exceção
try{
myChat= new Chat({
userID:'123AFK',
objVLD1:'teste1',
objVLD2:'teste2',
objVLD3:'teste3',
objVLD4:'teste4'
});
}catch(e){
if(e=='delay tem que ser number'){
//seu código para tratar a exceção de não ter setado o delay
alert('exemplo exceção');
}
}
</script>
</head>
<body>
</body>
</html> [/codebox]
na classe Chat eu fiz 4 método de suma importancia,
o startChat, para iniciar as requisições
o stopChat, para para as requisões
o setDelay para alterar o delay de requisão
o getStatus para ver se o chat esta on/off
dentro do startChat eu deixei um code de exemplo pra você alterar e colocar o seu code, tem mais códigos ai na classe que são para as outras varieveis que vc usa.
no html, eu crio uma varivel myChat e crio uma instancia da class Chat nela no onload, pois no onload todo html vai estar acessivel para vc manipular.
todos os parmetros são requeridos, se deixar de especificar um vai lançar um exceção javascript no browser, os métodos set também lançam exceções caso não sejam devidamente parametrizados.
Um abraço.
Edição feita por: Édipo Costa Rebouças, 27/01/2010, 23:56.