Sir
Sir

Reputation: 8280

Requests with JS - is this out dated?

I am making a basic live chat and was wondering if i have learnt this correctly...

I have my call function like this:

function call_data(url,data)
{
  if (window.XMLHttpRequest) {              
    AJAX=new XMLHttpRequest();              
  } else {                                  
    AJAX=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (AJAX) {
  querystring = "?dta="+data;
     AJAX.open("GET", url + querystring, false);                             
     AJAX.send(null);
     return AJAX.responseText;                                         
  } else {
     return false;
  }                                             
}   

function checker(id){
        result = parseInt(call_data('check_chat.php',id)); //check new messages
        if(result){//if new message
        loadchat(id); //load the messages
        } else {
        setTimeout(function() { checker(id); }, 5000); //check for new message every 5 seconds
        }
}

Is this the best way to call for new messages periodically ?

Upvotes: 0

Views: 52

Answers (1)

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

You are doing a synchronous call with XMLHttpRequest -- this causes the browser to freeze until data is returned. asynchronous are MUCH nicer and only a touch more complicated.

I'd recommend doing a bit more research yet.

I took a quick look at this link, it might help: http://www.cristiandarie.ro/asp-ajax/Async.html

Upvotes: 2

Related Questions