WEB Simple Ajax problem

Discussion in 'OT Technology' started by ge0, Jun 22, 2008.

  1. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    Ok, I am trying to create a realtime bar graph that gets updated ever few seconds.. but my problem is.. That it will work if I put an ALERT right before the ajax function call... but if I take the alert away it gives me an "unavailable" error in the firefox error console. So I figured it was trying to request data to fast for Ajax to handle it. So I tired putting a pause in the spot where the alert is. Still, no dice.. When I put the alert back in, it works.. Anyone have any ideas?
    Code:
    
    <script type="text/javascript">
    
    function pause(numberMillis) 
     {
      var now = new Date();
      
      var exitTime = now.getTime() + numberMillis;
       while (true) 
        {
         now = new Date();
          if (now.getTime() > exitTime)
           return;
          }
     }
    
    function realTime()
     {
       var rTime = new Array();
     <?php $g=0; do {
      ?>rTime[<?php echo $g; ?>] = <?php echo $row_dev3['PID']; ?>;
           <?php $g++; }while ($row_dev3 = mysql_fetch_assoc($dev3_result)); ?>
        var f=0;
     var totalNum = <?php echo $g-1; ?>
        while(true)
       {
       pause(3000); <---- Function will work if I replace this line with an ALERT.
       ajaxtest(rTime[f]);
       if(f==totalNum)
        {
         f = 0;
         break;
          }
        else
        {
         f++;
        }
               
            }
    
      }
    
    function GetXMLHttpRequest() 
    {
        var object = null;
        
        if (window.XMLHttpRequest) 
        {
            object = new XMLHttpRequest();
        } 
        else if (window.ActiveXObject) 
        {
            try
            {
                object = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e)
            {
            }
            
            if (object == null)
            {
                try
                {
                    object = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e)
                {
                }
            }
        }
        
        if (object == null)
        {
            alert("Your browser does not support AJAX");
        }
        
        return object;
    }
     
    
    function ProcessRequest() 
    {
        
    if(objectjax.readyState == 1) { 
     var pic = document.getElementById( 'loading' );
      pic.src = "searching.gif";
      }
      if (objectjax.readyState == 4) 
        {
            if (objectjax.status == 200) 
            {
               
       results = objectjax.responseText.split(",");
                var bHeight = results[0];
       var nQty = results[1];
       var wGraph = results[2];
        document.getElementById('testdiv').innerHTML = results[0];
       document.getElementById(wGraph).innerHTML = nQty + '<img src="column.gif" width="50" height="' + bHeight + '" />';
      
    
            } 
            else
            {
                alert("error");
            }
        }
    }
    
    function ajaxtest(parm)
      {
      
      
      var url="test1.php?pid="+parm;
      objectjax.open("GET", url, true);
      objectjax.onreadystatechange = ProcessRequest;
      objectjax.send(null);
    
        }
    var objectjax = GetXMLHttpRequest();
    </script>
     
    
    
    ERROR
    Error: uncaught exception: [Exception... "Component returned failure code: 0xc1f30001 (NS_ERROR_NOT_INITIALIZED) [nsIXMLHttpRequest.send]" nsresult: "0xc1f30001 (NS_ERROR_NOT_INITIALIZED)" location: "JS frame :: test.php :: ajaxtest :: line 159" data: no]
     
  2. projectalpha

    projectalpha 二號 Stunna

    Joined:
    Aug 4, 2001
    Messages:
    17,671
    Likes Received:
    0
    Location:
    Bay Area, California
    why don't you use setTimeout?
     
  3. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    hmmm.. let me try that
     
  4. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    nope.. Unless I did it wrong


    setTimeout("ajaxtest(" + rTime[f] + "), 3000);


    but if I add an ALERT in front of the setTimeout, it works like a charm.
     

Share This Page