Jump to content

[SOLVED] Using load function from within loop


mg.83

Recommended Posts

Hi All,

 

I'm trying to send multiple requests to a php page (via a loop) but am having issues with the receiving page processing too slow. As a result the loop sends through requests before they can be processed by the recieving page. What I am looking for is a way to make the script wait until each request is processed before sending the next one. any help would be greatly appreciated.

 

code on sending page is below:

 

for(var i=0; i<cartSerial.length-1; i++) {

//call to updateacknowledgeaction.php

jx.load('updateacknowledgeaction.php','ttbl=<?php echo $timetable,'POST', function(data){

eval(data);

 

//do stuff with eval'ed data

});

 

}

 

 

code for jx (js file) is below:

 

jx = {

http:false, //HTTP Object

format:'text',

callback:function(data){},

error:false,

//Create a xmlHttpRequest object - this is the constructor.

getHTTPObject : function() {

var http = false;

//Use IE's ActiveX items to load the file.

if(typeof ActiveXObject != 'undefined') {

try {http = new ActiveXObject("Msxml2.XMLHTTP");}

catch (e) {

try {http = new ActiveXObject("Microsoft.XMLHTTP");}

catch (E) {http = false;}

}

//If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.

} else if (XMLHttpRequest) {

try {http = new XMLHttpRequest();}

catch (e) {http = false;}

}

return http;

},

// This function is called from the user's script.

//Arguments -

// url - The url of the serverside script that is to be called. Append all the arguments to

// this url - eg. 'get_data.php?id=5&car=benz'

// callback - Function that must be called once the data is ready.

// format - The return type for this function. Could be 'xml','json' or 'text'. If it is json,

// the string will be 'eval'ed before returning it. Default:'text'

load : function (url,params,method,callback) {

this.init(); //The XMLHttpRequest object is recreated at every call - to defeat Cache problem in IE

 

if(!this.http||!url) return;

if (this.http.overrideMimeType) this.http.overrideMimeType('text/xml');

this.callback=callback;

var ths = this;//Closure

if(method=="GET"){

url += "\?" + params;

}

 

 

 

if (this.http.overrideMimeType) this.http.overrideMimeType('text/xml');

 

//Kill the Cache problem in IE.

var now = "uid=" + new Date().getTime();

url += (url.indexOf("?")+1) ? "&" : "?";

url += now;

 

this.http.open(method, url, true);

 

this.http.onreadystatechange = function () {//Call a function when the state changes.

if(!ths) return;

var http = ths.http;

if (http.readyState == 4) {//Ready State will be 4 when the document is loaded.

if(http.status == 200) {

var result = "";

if(http.responseText) result = http.responseText;

 

//Give the data to the callback function.

if(ths.callback) ths.callback(result);

} else { //An error occured

if(ths.error) ths.error()

}

}

}

if(method=="GET"){

this.http.send(null);

} else {

this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

this.http.setRequestHeader("Content-length", params.length);

this.http.setRequestHeader("Connection", "close");

 

this.http.send(params);

}

},

init : function() {this.http = this.getHTTPObject();}

}

 

the receiving page updates a database (which is not structured too well - hence the slow processing)

 

 

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.