Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Miguel Moure

AJAX muito lento no IE

Recommended Posts

Boa tarde,

estou com um problema serio de perfomace no meu script de atualização. Como pode ver, ele é executado a cada 1,5 segundos, porem na div de resposta não aparece nada.. so depois de 20 segundos.. isso so acontece no IE e nos demais browser é tudo bem rapido. Já tentei de tudo, porem nada resolveu... gostaria de saber se alguém sabe resolver isso..meu codigo esta abaixo:

var ajaxRequest;
url = 'ajaxAtualizaChat.php';
function atualizaChat()
{
try
{ //opera,firefox...
ajaxRequest=new XMLHttpRequest();
ajaxRequest.onreadystatechange = atualizaChatVer;
ajaxRequest.open("GET",url,true);
ajaxRequest.send(null);
//alert("FF!");

}
catch(e)
{
try{
//var axO=['Msxml2.XMLHTTP','Msxml2.XMLHTTP.3.0', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.4.0' ];

ajaxRequest= new ActiveXObject("Msxml2.XMLHTTP.4.0");
if (ajaxRequest)
{
ajaxRequest.onreadystatechange = atualizaChatVer;
ajaxRequest.open("GET",url,true);
ajaxRequest.send();

}

}catch(e)
{
try{
ajaxRequest= new ActiveXObject("Microsoft.XMLHTTP");
ajaxRequest.onreadystatechange = atualizaChatVer;
ajaxRequest.open("GET",url,true);
ajaxRequest.send();

}
catch(e){
alert("f****!");
return false;
}
}

}
setTimeout('atualizaChat()',1500);
}

function atualizaChatVer()
{

//Verificando se o estado é igual a 4, ou seja, requisição completada com sucesso.
if (ajaxRequest.readyState == 4)
{
//Em caso de requisição completa, atualiza tela de chat
document.getElementById('centro-chat').scrollTop = document.getElementById('centro-chat').scrollHeight;
document.getElementById('centro-chat').innerHTML = ajaxRequest.responseText;
}
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

Um dos primeiros problemas está em instanciar um novo objeto e configurá-lo a cada vez que se faz a requisição.

 

Outro problema está em passar a função no setTimeout como string, essa string será avaliada antes de ser executada.

 

Por último, você tem 2 catches embutidos, o que faz gerar o erro, tratar, gerar um novo erro e tratar. Erros nunca são bons, mesmo que possível tratá-los, evite.

 

tente algo nesse teor:

if(!window.XMLHttpRequest) var XMLHttpRequest = function(){
	var tryes = new Array("MSXML2.XMLHTTP.7.0", "MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP");
        var ret = null;
	do {
		try {
			ret = new ActiveXObject(tryes[0]);
		} 
		catch (e) {
			tryes.splice(0, 1);
		}
	}
	while (ret == null && tryes.length);
        if(ret == null) alert('Sem suporte a AJAX!');
        return ret;
}

var AJAX = new XMLHttpRequest;
AJAX.onreadystatechange = atualizaChatVer;
AJAX.open("GET",url,true);
setInterval(AJAX.send, 1500);

Compartilhar este post


Link para o post
Compartilhar em outros sites

Evandro, então... coloquei o que você fez.. fico assim..

if(!window.XMLHttpRequest) var XMLHttpRequest = function(){
        var tryes = new Array("MSXML2.XMLHTTP.7.0", "MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP");
        var ret = null;
        do {
                try {
                        ret = new ActiveXObject(tryes[0]);
                } 
                catch (e) {
                        tryes.splice(0, 1);
                }
        }
        while (ret == null && tryes.length);
        if(ret == null) alert('Sem suporte a AJAX!');
        return ret;
}


url = 'ajaxAtualizaChat.php';
var AJAX = new XMLHttpRequest;
AJAX.onreadystatechange = atualizaChatVer;
AJAX.open("GET",url,true);
setInterval(AJAX.send, 1500);

function atualizaChatVer()
{
	if (AJAX.readyState == 4)
	{
		document.getElementById('centro-chat').scrollTop = document.getElementById('centro-chat').scrollHeight;		
		document.getElementById('centro-chat').innerHTML = AJAX.responseText;
	}
}
mas assim da esse erro no console de erro:

 

Erro: uncaught exception: [Exception... "Illegal operation on WrappedNative prototype object" nsresult: "0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "native frame :: <unknown filename> :: <TOP_LEVEL> :: line 0" data: no]

 

 

O que pode ser?

Abraços.. e obrigadão!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Rapaz, pra que loop pra instanciar o objeto XMLHTTPRequest?

function GetXmlHttpObject(){
 //cria o objeto XMLxmlHttpRequest pra Firefox, Chrome, Opera, Safari, etc.
 try {
 return new XMLHttpRequest();
 } 
 
 //cria o objeto XMLHttpRequest pra IE 6.0 e posteriormente para IE7+
 catch (e) {
 try {
 return new ActiveXObject('Msxml2.XMLHTTP');
 } 
 
 catch (e) {
 return new ActiveXObject('Microsoft.XMLHTTP');
 }
 }
 
 };

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.