Jump to content

Progress Bar, xmlHttpRequested scripts not running together (simultaneously)


nmullen

Recommended Posts

Hey everyone, I've been working on a progress bar which uses session variables and xmlHttpRequests to increment its self. The current problem that I have been experiencing is that when I use an xmlHttpRequest to call one of my php scripts in the midst of calling a different php script one finishes then the other finishes, but never run at the same time.

 

Am I calling my progressBar() at a wrong location or are my xmlHttpRequests not built to run multiple scripts at once?  Here's my javascript code (Inside PHP tags for easier readability).  I'd post portions but in order to understand the issue one has to follow the flow of the program.

 

The progressBar() is called in the getGooglePages() function once the xmlHttp ready state of my first script is 1.  Once progressBar() is called it uses my second script to get the session variables which are concurrently set in my first.

 

Any ideas?

 

var xmlHttp = new Array(3);
var Pages = new Array();
var KWs = new Array();
var source = new Array();
var results;
var progression;
var interval;
var progxmlHttp;

//Automate Button
function Automate()
{    
    var url = "";
    var index = 0;
    var i;
    var list = document.getElementById("lstKWs");
    
    //Automate Clicked
    progression = 1;
    
    //Fills the keyword list
    FillKWs();
    
    //If no keywords are present
    if (list[0] == null || list[0].text == "---Click to Populate---")
    {
        alert("Please Enter Keywords");
        document.getElementById("txtKWs").focus();
        return false;
    }
    

    initializeArrays();
    //interval = setInterval(progressBar, 100);

    //Initializes the xmlHttp Object
    xmlHttp=GetXmlHttpObject();
    
    //Opens phpCall
    //Starts a chain of calling sequences
    var url="_includes/phpCall.php";
    //Calls the adjust pagerank in waiting for a readyState = 4
    xmlHttp[0].onreadystatechange=AdjustPageRank;
    xmlHttp[0].open("GET",url,true);
    xmlHttp[0].send(null);
}


//Initializes all the arrays
function initializeArrays()
{
    var i;
    var j;
    var list = document.getElementById("lstKWs");
    
    //Gather all the keywords and place them into an array
    KWs.length = list.length;
    for (i=0;i<KWs.length;i++)
        KWs[i] = list[i].text.toLowerCase();
        
    //Resize Page array
    Pages.length = KWs.length;
    for (i=0;i<KWs.length;i++)
        Pages[i] = new Array(); //Resizes each page array to an additional array to make multidimensional
        
    //Sets the source array up
    source.length = KWs.length;
    for (i=0;i<KWs.length;i++)
        source[i] = new Array();
}


function FillKWs()
{
    var list = document.getElementById("lstKWs");
    
    //Validating the text box
    if (document.getElementById("txtKWs").value.length<1||!document.getElementById("txtKWs").value.match(/[^s]/))
    {
        alert("Please enter keywords!");
        list.add(document.createElement('option'), null);
        list[0].text = "---Click to Populate---";
        return;
    }
    
    var ray=document.getElementById("txtKWs").value.split(/r?n/g);
    
    var i;
    for (i=0;i<ray.length;i++)
        if (ray[i] == "google" || ray[i] == "yahoo" || ray[i] == "msn" || ray[i] == "alltheweb")
        {
            alert("Invalid keyword(s), search engine name found in keywords");
            return;
        }
    
    list.length = 0;
    
    for (i=0;i<ray.length;i++)
    {
            var y=document.createElement('option');
                
            if (ray[i] !== "")
            {
              y.text=ray[i];
                
          try
            {
                list.add(y,null); // standards compliant
            }
          catch(ex)
            {
                list.add(y); // IE only
            }
            }
    }
}

//Progress Bar
function progressBar(){
  var url = "_includes/getProgress.php";// init - just clean up a few things to start
  var ele = document.getElementById("ProgressPercent");
  var progress = 0;
  var debug = true;// turn off again later 
  progxmlHttp = getSingleXMLHttpObject();
  
  while(ele.hasChildNodes())
    ele.removeChild(ele.firstChild);
  ele.appendChild(document.createTextNode('Loading: 0%'));

    if (progress >= 100)
        alert("PROGRESS IS >= 100");

  while(progress != 100){// the loop
    progxmlHttp.open("GET", url, false);// send request
    progxmlHttp.send(null);

    if(debug){
       if( ! confirm("Status: "+progxmlHttp.status+"\nresponsetext: "+progxmlHttp.responseText+"\n\ncontinue?")){
         return;
       }
    }
    if(progxmlHttp.status != 200){// if there's an error
      alert("error\nresponse code: "+progxmlHttp.status+"\nerror message: "+progxmlHttp.responseText);
       return;
    }
    progress = parseInt(progxmlHttp.responseText, 10);// load response
    ele.removeChild(ele.firstChild);// empty old text
    ele.appendChild(document.createTextNode('Loading: '+progress+'%'));// show new text
  }

// cleanup etc.
  alert('loading done.');
}

//Adjusts Page Rank
function AdjustPageRank(rank)
{

    var index;
    var rank;
    
    if (xmlHttp[0].readyState==4)
    {
    
        rank = xmlHttp[0].responseText;
    
        //Depending on page rank update radio buttons
        if (rank >6.9)
            document.getElementById("Linking_PageRank_0").checked = true
        else if (rank > 3.9)
            document.getElementById("Linking_PageRank_1").checked = true
        else
            document.getElementById("Linking_PageRank_2").checked = true
            
        
        //Calling the getGooglePages and concatenating the length of the keywords
        url="_includes/getGooglePages.php?KWLength=" + KWs.length;
        
        //Puts the array of keywords onto the URL
        for (index = 0;index<KWs.length;index++)
            url += "&keyword" + index + "=" + KWs[index];
            
            
            //When the state changes call the getPages
            xmlHttp[1].onreadystatechange = function(){ getGooglePages();};
            xmlHttp[1].open("GET",url,true);
            xmlHttp[1].send(null);
            
            
    }
}

//Gets the google search results in addition to the source code for the searched pages
function getGooglePages()
{
    var i;
    var j;
    var tempArray;
    
    if (xmlHttp[1].readyState == 1)
        progressBar();              //---CALLING THE PROGRESS BAR---
    
    //When everything is complete
    if (xmlHttp[1].readyState == 4)
    {
        //URL's are seperated by "
        results = xmlHttp[1].responseText;
        
        //Parses the string and grabs all URL's
        tempArray = results.split("|||||");
        
        //If there are invalid URL's at the end of the array it destroys them until it finds valid ones!
        while(tempArray[tempArray.length-1].indexOf(".") < 0)
            tempArray.length = tempArray.length-1;
        
            
        //Since we are only grabbing first 10 results per page we require
        //a continuous counter to just increment through the list and
        //counters to keep track of the allocations in the arrays
        var ContinuousCounter = 0;
        for(j=0;j<KWs.length;j++)
            for(i=0;i<10;i++)
            {
                if (tempArray[ContinuousCounter] != null && tempArray[ContinuousCounter] !== "undefined")
               {
                    Pages[j][i] = tempArray[ContinuousCounter] //Places the pages per-Keyword into the appropriate slot
                    source[j][i] = tempArray[ContinuousCounter+1].toLowerCase()  //Grabs the source code
                    ContinuousCounter+=2;    //The items are placed right after eachother: URL - Source - URL - Source etc.
                                            //So that requires it to be incremented by 2
               }
            }
            
    }
    
}

function GetXmlHttpObject()
{
    var i = 0;

    
    try
      {
      // Firefox, Opera 8.0+, Safari
          //Page Rank
          xmlHttp[0]=new XMLHttpRequest();
        
        //Google Search Pages
        xmlHttp[1] = new XMLHttpRequest();
        
        //Source Code Retrieval
        xmlHttp[2] = new XMLHttpRequest();
        
        //Progress Bar
        progxmlHttp = new XMLHttpRequest();
      }
    catch (e)
      {
      // Internet Explorer
      try
        {
                //Page Rank
                xmlHttp[0]=new ActiveXObject("Msxml2.XMLHTTP");
                
                //Google Search Pages
                xmlHttp[1]=new ActiveXObject("Msxml2.XMLHTTP");
                
                //Source Code Retrieval
                xmlHttp[2]=new ActiveXObject("Msxml2.XMLHTTP");
                        
                //Progress Bar
                progxmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (e)
        {
                //Page Rank
                xmlHttp[0]=new ActiveXObject("Microsoft.XMLHTTP");
                
                //Google Search Pages
                xmlHttp[1]=new ActiveXObject("Microsoft.XMLHTTP");
                
                //Source Code Retrieval
                xmlHttp[2]=new ActiveXObject("Microsoft.XMLHTTP");
                
                //Progress Bar
                progxmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      }
    return xmlHttp;
} 

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.