/    Sign up×
Community /Pin to ProfileBookmark

XMLHttpRequest: How Do I Create Seperate Instances So Variables Don’t Overlap

I’m VERY new to Javascript but I am trying to implement the [COLOR=Red]XMLHttpRequest[/COLOR] feature in one of my applications. The problem is I can only run it in [B]synch[/B]ronous mode becuase when I use it in [B]async[/B]hronous I think some how the variables are overwritting. I for the life of me don’t know how to implement this such that each call to the function creates a seperate instance so I can have multiple calls on the same page at the same time.

So the below code works synchronously and because only one request is returned at a time, then the next it does not foul up multiple simultaneous calls to the same functions. I’m looking for a savior so I can effectively implement this the way it was supposed to work. Code below:

[B]JAVASCRIPT FILE:[/B]

function createRequestObject() {

var req;

if(window.XMLHttpRequest){
// Firefox, Safari, Opera…
req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
// Internet Explorer 5+
req = new ActiveXObject(“Microsoft.XMLHTTP”);
} else {
// There is an error creating the object,
// just as an old browser is being used.
alert(‘There was a problem creating the XMLHttpRequest object’);
}

return req;

}

function handleDivTag(divtag)
{
var divtag;
return divtag;
}

function handleTimeOut(timeout)
{
var timeout;
return timeout;
}

function handleRefresh(refresh)
{
var refresh;
return refresh;
}

// Make the XMLHttpRequest object
var http = createRequestObject();

// Create the Divtag Handler — Mainly an IE 6 Fix
var divhandler = new handleDivTag(null);
var timehandler = new handleTimeOut(null);
var refreshhandler = new handleRefresh(null);

function sendRequest(act,divtag,timeout,refresh)
{
// Open PHP script for requests in sycnhronous mode
http.open(‘get’, act, [COLOR=Red]0[/COLOR]);
http.onreadystatechange = function () { handleResponse(); }
divhandler.divtag = divtag;
timehandler.timeout = timeout;
refreshhandler.refresh = refresh;
http.send(null);
}

function handleResponse()
{
if(http.readyState == 4 && http.status == 200)
{
// Text returned FROM PHP script
var response = http.responseText;
// document.write(http.responseText);
if(response)
{
// UPDATE ajaxTest content
document.getElementById(divhandler.divtag).innerHTML = response;
setTimeout(timehandler.timeout,refreshhandler.refresh);
}
}
}

function news() {sendRequest(‘/ping/panel_c_news’,’news’,news,120000);}
function blogs() {sendRequest(‘/ping/panel_c_blogs’,’blogs’,blogs,300000);}
function activities() {sendRequest(‘/ping/panel_c_activities’,’activities’,activities,60000);}
//function whoswho() {sendRequest(‘/ping/panel_c_whoenss_downloads’,’whoswho’,whoswho,600000);}
function whosonline() {sendRequest(‘/ping/panel_l_whosonline’,’whosonline’,whosonline,30000);}
function whosout() {sendRequest(‘/ping/panel_l_whosout’,’whosout’,whosout,60000);}

[B]MY FUNCTIONS CALLS FROM MY HTML FILE:[/B]

<script language=”JavaScript”>
function LoadFuncs()
{ news();blogs();activities();whosonline();whosout() }
</script>

<BODY onload=”LoadFuncs()”>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@nzakasJan 27.2006 — Before anyone can help, you'll need to explain exactly what it is that you're trying to accomplish.
Copy linkTweet thisAlerts:
@cluettrauthorJan 27.2006 — Thank you for the quick response.

I am trying to have multiple <DIVS> reside on one HTML page so the content will refresh using the javascript timeout function. So I might have 5 different <DIV> statements on the same HTML page that need to have the data refreshed every 2 minutes.

Your thoughts?
Copy linkTweet thisAlerts:
@A1ien51Jan 27.2006 — You need to make it Object Oriented. I would look at prototype.js or code like that in my book Ajax In Action.

Eric
×

Success!

Help @cluettr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...