Jump to content

[SOLVED] Simultaneous Ajax Requests


dpacmittal

Recommended Posts

I am making a chat script. I've got two Ajax requests which i want to work simultaneously.

The first request checks for new messages and displays them using long polling method (Thanks to rhodesa for teaching me). The second one check users online and display them in the users online list every 5 seconds.

 

I can get the both to work with frames. But I cant get them to work If I put them on single page.

 

I tried using

function startRetrieve() {
setInterval("fetchMsg()",500);
setInterval("fetchUsers()",5000);
}

 

and in the html part:

<body onload="startRetrieve()">

 

but only 1st request seems to work. I tried the frames and got the both working but I don't want to use frames on my website.

What is causing this problem? where am i wrong?

Link to comment
Share on other sites

you're probably overwriting the xmlhttp request object.

 

you would need to have two of them for that.

 

Suppose your xmlhttp request object is named ajax, then you should make a second one named ajax2 or something different.

 

Thanks, It solved my problem. One question though, why does it behaves like this?

 

Link to comment
Share on other sites

it's because everything you want to do with a single xmlhttp request object is anchored to that one object. if you send out a request using a variable named "ajax" as your xmlhttp request object, and then overwrite that variable with something else, you lose your anchor.

 

If you have a function that is creating an ajax object for you, make it return a new object.

 

return new ajax;

 

like so (it might take a bit more than that depending on the way you have your function setup, but if you do have a function that creates the object for you, make sure to return a new one of what you make ;) )

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.