SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 28
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Italy
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Interrupting AJAX calls

    Hi,
    A big thanks to all who can help me.

    I am working on a Firefox extension which, on every page load, will do a number of AJAX calls to check some information. If the servers are slow to respond, or if there are too many calls being made, the user cannot click out of the page. Basically he has to wait for all the calls to finish naturally. Does anybody know if there a way to interrupt all AJAX calls once a certain event is fired?


    Thanks for your help,
    frodo

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Italy
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i apologise for doing this, but ...bump?

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could maintain a pool/list of ajax connections, and loop them and call abort() on them all in your event handler.

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Italy
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi crmalibu,
    thanks for your feedback. how do i create a list of ajax connections. i think this is a basic questions, but i'm reasonably a beginner with ajax.

    thanks,
    frodo

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var list = [];
    Each time you create a new XMLHttpRequest object, store a reference to it in the list.
    Code:
    var obj = new XMLHttpRequest();
    list.push(obj);

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Italy
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks crmalibu,
    if i understand correctly i then loop through the list and abort them like this, correct?

    Code:
    list[i].abort
    thanks,
    frodo

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    abort() is a method, not a property. So you could call it like
    list[i].abort()

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Italy
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much crmalibu. i'll test and let you know!

  9. #9
    SitePoint Addict Limes102's Avatar
    Join Date
    Sep 2006
    Location
    North Wales
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Out of interest, did you get this working?

    I'm having pretty much exactly the same problem but with push notifications.

    Thanks

  10. #10
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Italy
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi mr j, unfortunately i have not yet had the opportunity to test this, but i will do so asap and will report here my results.


    frodo

  11. #11
    SitePoint Addict Limes102's Avatar
    Join Date
    Sep 2006
    Location
    North Wales
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, thank you

    This one really has stumped me. I've tried nearly everything. I've set variables to try and stop the readyState 4 from being set when I reload or click off the page, and even that didn't work because of it's `asynciness`

    Facebook has managed it with persistent connections, as soon as you reload the page, instantly, the connections are dropped.

    Anyway,
    Thanks

    Aaron

  12. #12
    SitePoint Addict Limes102's Avatar
    Join Date
    Sep 2006
    Location
    North Wales
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On another thread I found that I don't really need to abort the request. The reason why it wasn't working was because I had a php session in two scripts at once, and it didn't like this.

    Make sure you haven't got a session_start(); on the page you're requesting with ajax


  13. #13
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Italy
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    unfortunately this does not seem to be working for me.
    i would love to share the link on which i am workgin, but i am afraid to be considered a spammer by doing so.

  14. #14
    SitePoint Addict Limes102's Avatar
    Join Date
    Sep 2006
    Location
    North Wales
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you show the main code of both scripts?
    And the JavaScript that makes the request

    Still might be able to help maybe

  15. #15
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Italy
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi mr j,
    thank you very much for your help. i am developing an addon for firefox which collects information about javascript and css files of the page you are visiting. the information is collected via an ajax call where the url variable is the url of the file in question. the problem seems to occour with pages with 40+ files where the addon hangs and does not allow to switch tab, click on the links, close the tab...

    the part of the code giving problems is the following (in reality it is wrapped in a function):
    Code JavaScript:
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open ('HEAD', url, false);
     
    xmlhttp.send (null);
     
    if (xmlhttp.status == "304")
        var cached = true;
     
    if (xmlhttp.getResponseHeader('Content-Encoding') == 'gzip')
        var gzipped = true;
     
    if (xmlhttp.getResponseHeader('Content-Length'))
        var size = xmlhttp.getResponseHeader('Content-Length');
     
    var headers = xmlhttp.getAllResponseHeaders();

    once more thanks for your help mr j


    frodo

  16. #16
    SitePoint Addict Limes102's Avatar
    Join Date
    Sep 2006
    Location
    North Wales
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hiyah

    Is this function called multiple times? So that you process multiple files at once?

  17. #17
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Italy
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mr. J View Post
    Hiyah

    Is this function called multiple times? So that you process multiple files at once?
    correct, it is called once per every file. if a page has 50 js files (this sometimes happens), the function is caled 50 times


    frodo

  18. #18
    SitePoint Addict Limes102's Avatar
    Join Date
    Sep 2006
    Location
    North Wales
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So this is happening asynchronously ... What I would suggest is adding a small pause of about 200 milliseconds, to give the function time to download the page... Or it will eventually crash the browser ...

    setTimeout('function(url)', 200);

    That will wait 200 seconds between calling the function, so less change of crashing the browser, the other thing you could do is not doing it in a function, and looping through each file... when you call the function, it'll keep calling it... untill it crashes it
    Hope that helps

  19. #19
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Italy
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mr j, you are a genious. i never thought of the timout trick, i just implemented it and it works a treat!!!
    i am not sure i understand your second suggestion though


    thanks,
    frodo

  20. #20
    SitePoint Addict Limes102's Avatar
    Join Date
    Sep 2006
    Location
    North Wales
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rather than using a function

    loop
    get page
    do your processing
    end loop

    rather than calling a function for each page.. I imagine the second method will be faster, as the file might load faster than 200 miliseconds

    I'm glad I could help though

    Aaron

  21. #21
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Italy
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it was brilliant, seriously, thanks!

  22. #22
    SitePoint Addict Limes102's Avatar
    Join Date
    Sep 2006
    Location
    North Wales
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem... I spent about 20 house in total trying to make these ajax requests work.... in the end i got there, but i learnt a lot in the process

    glad i could help thuogh

  23. #23
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mr. J View Post
    On another thread I found that I don't really need to abort the request. The reason why it wasn't working was because I had a php session in two scripts at once, and it didn't like this.

    Make sure you haven't got a session_start(); on the page you're requesting with ajax

    There's no reason you can't use session_start() in an AJAX back end script. You merely need to do what you'd normally do in a PHP script: make sure nothing is output to the browser before calling session_start().

  24. #24
    SitePoint Addict Limes102's Avatar
    Join Date
    Sep 2006
    Location
    North Wales
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get the problem because the script continues to run as a loop until there is a response, a page under that session was already in the process of being generated, so it wouldn't allow it to generate another page under that session... at least that seems to be what was happening... Removing it did seem to fix my problems

  25. #25
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The default files save handler for sessions makes sure that only a single script can access a specific session file at a time. This is done to prevent data corruption, and potentially reduce the programmer wtf factor. The call to session_start() will block(hang) until its able to acquire an exclusive read+write lock on the session file.

    If you have a script that will execute for a long time, and it needs to use session, you should just take care to only open the session for a short period of time. You can close and reopen if needed
    eg
    PHP Code:
    session_start();
    $_SESSION['foo'] = 5;

    // were done modifying session variables for now. 
    // let go of the lock and save session vars
    session_write_close();

    // do something that takes a very long time
    download_images();

    //optionally, reopen the session if needed so we can write more vars
    // beware, another script may have changed the variables
    session_start(); 
    ps, php calls session_write_close() automatically for you at the end of execution, if that helps clarify the picture. Similar to database connections, sometimes you just cant't wait that long to close it.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •