Multiple AJAX Connections Running at Once... Possible?

Discussion in 'JavaScript' started by LiNx88, Oct 21, 2006.

  1. #1
    Hey..

    I'm trying to have more than 1 ajax request running on my site at a time, but it seems that as soon as a second request starts, the first one stops... I don't know if it's a limitation of javascript or its something I'm doing wrong, but if somebody could help me out that would be great...

    Thanks in advance
    LiNx88

    <script>
    function AjaxConnection(url) 
    {
       this.connect=connect;
       this.uri=url;
    } 
    function connect(return_func)
            {
                with(this)
                {
                    x=init_object();
                    x.open("POST", uri,true);
                    x.onreadystatechange = function() {
                            if (x.readyState != 4)
                                    return;
                            eval(return_func + '(x.responseText)');
                            delete x;
                    }
                    x.setRequestHeader('Content-Type',
                        'application/x-www-form-urlencoded');
                    x.send('');
                }
            }
    function init_object() {
            var x;
            try {
                    x=new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                    try {
                            x=new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (oc) {
                            x=null;
                    }
            }      
            if(!x && typeof XMLHttpRequest != "undefined")
                    x = new XMLHttpRequest();
            if (x)
                    return x;
    }
    </script>
    
    
    <script>
    function clickme() {
        connection = new AjaxConnection("text.txt");
        connection.connect("callBack");
    }
    function clickme2() {
        connection2 = new AjaxConnection("big.txt");
        connection2.connect("callBack");
    }
    function callBack(content) {
            document.getElementById('hi').value = content;
    }
    </script>
    Code (markup):
     
    LiNx88, Oct 21, 2006 IP
  2. LiNx88

    LiNx88 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I fixed it.. For future reference and if anybody's the least bit interested, the problem was with this function

                    this.x.onreadystatechange = function() {
                            if (self.x.readyState != 4)
                                    return;
                            eval(return_func + '(self.x.responseText)');
                            delete self.x;
                    }
    Code (markup):
    For some reason, the x's inside the new function were relating to the global scope instead of just the instance of the function... When I changed it to this

    function connect(return_func)
            {
                    this.x=init_object();
                    this.x.open("GET", this.uri,true);
                    var self = this;
                    this.x.onreadystatechange = function() {
                            if (self.x.readyState != 4)
                                    return;
                            eval(return_func + '(self.x.responseText)');
                            delete self.x;
                    }
                    this.x.setRequestHeader('Content-Type',
                        'application/x-www-form-urlencoded');
                    this.x.send('');
            }
    Code (markup):
    Everything worked perfectly.

    -LiNx88
     
    LiNx88, Oct 21, 2006 IP
  3. vannazz

    vannazz Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    LiNx88! you are absolutely brilliant!! i've been searching for so long to make multiple ajax request at the same time work but nobody gave a good solution except for you! i used your code and everything worked perfectly! thank you very much!

    vannazz
     
    vannazz, Sep 10, 2008 IP
  4. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The A in Ajax stands for Asynchronous. So really, almost by definition you should be able to have multiple requests running simultaneously.

    To the original poster: why aren't you using a library?

    Whoa, I just realized how old the original post is.
     
    LogicFlux, Sep 10, 2008 IP
  5. koolman

    koolman Peon

    Messages:
    76
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    koolman, Sep 10, 2008 IP
  6. Shadowcoder

    Shadowcoder Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I registered here for the sole purpose of responding to your 5 year-old post to say simply,

    That was masterful. Thank you
     
    Shadowcoder, Oct 7, 2011 IP