Ben basit bir ajax işlevi, böyle bir şey var:
var x;
var myRequest = new Array();
function CreateXmlHttpReq(handler) {
var xmlhttp = null;
try {
xmlhttp = new XMLHttpRequest();
}catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
xmlhttp.onreadystatechange = handler;
return xmlhttp;
}
function getResults(){
var r = Math.random();
var someVar = document.getElementById('myvar').value;
var myUrl = 'url/of/my/phpScript.php?';
myUrl += 'r=' + r;
//encodeURIComponent() instead of escape() when i aspect normal text
myUrl += '&someVar=' + escape(someVar);
//startLoading just show an overlay with a small rotating gif
startLoading();
x++;
myRequest[x] = CreateXmlHttpReq(function() {printResultHandler(x)});
myRequest[x].open("GET", myUrl);
myRequest[x].send(null);
}
//example handler
function printResultHandler(x) {
if(myRequest[x].readyState == 4 && myRequest[x].status == 200){
//usually i use innerHTML for quick requests, the DOM for more complex req
document.getElementById(div).innerHTML = myRequest[x].responseText;
//this will hide the overlay showed ith startLoading()
stopLoading();
}
}
12345678901234567890123456789012345678901234567890123456789012345678901234567890
and that works fine. I just have some problems when the return flux is big (it can be xml, html, or whatever), the browser seem to 'fall asleep' for a while. I don't like to have a big amount of text (xml, html) all in one. It isn't nice to handle that.
I'm wondering if there exists some way to buffer that request. When the request is done and returns the 200 status, is there a way to get the responseText piece by piece (lets say 2048 bytes, or line by line)? I suppose something like:
function printResultHandler(x) {
if(myRequest[x].readyState == 4 && myRequest[x].status == 200){
//usually i use innerHTML for quick requests, the DOM for more complex req
//document.getElementById(div).innerHTML = myRequest[x].responseText;
var answer;
while(answer = readline(myRequest[x].responseText)){
//to something;
}
//this will hide the overlay showed ith startLoading()
stopLoading();
}
}
Kısacası, php readdir () veya fread () eşdeğer.