AJAX setTimeout

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JamieHowarth0
    Recognized Expert Contributor
    • May 2007
    • 537

    AJAX setTimeout

    Hi folks,

    I have a bit of a headache. I've finally added all the nice finishing touches to my own website (static only with a bit of DHTML).
    Now I've just converted the whole thing to AJAX with a couple of simple functions and links changed here and there.
    However, I want to create a really cool effect with my AJAX - which is I want to induce a "delay" so people actually see the "loading" section, cause at the moment, it's doing it at about 0.01ms and flashes across the screen before you have a chance to read it.

    [CODE=javascript]
    function GetXmlHttpObjec t() {
    var xmlHttp=null;
    try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest( );
    } catch (e) {
    // Internet Explorer
    try {
    xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" );
    } catch (e) {
    xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP");
    }
    }
    return xmlHttp;
    }

    var myBodyAjax = new GetXmlHttpObjec t();
    var myOCAjax = new GetXmlHttpObjec t();
    var myURL, myParams, myMethod, myContainer, myWaitingContai ner, myAJObject
    var thisAJ, thisCont, thisWaitCont
    var myAJ, myCont, myWait

    function doAjax(myURL, myParams, myMethod, myContainer, myWaitingContai ner, myAJObject) {
    if (myAJObject==nu ll) {
    alert ("Your browser does not support AJAX!");
    return;
    }
    myAJObject.onre adystatechange= function(){stat eChanged(myAJOb ject, myContainer, myWaitingContai ner)};
    switch (myMethod) {
    case "get":
    myAJObject.open ("GET",myURL+"? "+myParams,true );
    myAJObject.send (null);
    break;
    case "post":
    myAJObject.open ("POST",myURL,t rue);
    myAJObject.send (myParams);
    break;
    default:
    alert("AJAX data method not specified - please email webmaster@cmdev-associates.com" );
    }
    myAJObject.clos e;
    }

    function stateChanged(th isAJ, thisCont, thisWaitCont) {
    switch (thisAJ.readySt ate) {
    case 4:
    var timer = setTimeout('fun ction(){showCon tent(thisAJ,thi sCont,thisWaitC ont)}',2000);
    break;
    default:
    thisWaitCont.st yle.visibility = "visible";
    }
    }

    function showContent(myA J, myCont, myWait) {
    myWait.style.vi sibility='hidde n';
    myCont.innerHTM L = myAJ.responseTe xt;
    }
    [/CODE]

    The problem is happening at setTimeout - for whatever reason, it's not being called. I've tried taking the parameters out of quotes but they're AJAX and HTML objects and so aren't handled correctly, and I've used the function() part to force the setTimeout method to accept the function parameters, but still to no avail.
    Any help would be much appreciated.

    Many thanks,

    Benjamin
  • JamieHowarth0
    Recognized Expert Contributor
    • May 2007
    • 537

    #2
    Hi all,

    I solved the problem myself! I added a section of ASP code to my AJAX content-serving page:
    [CODE=asp]
    Dim startTime
    startTime = Now()
    Do While DateDiff("s",st artTime,Now()) < 5
    'Do absolutely nothing!
    Loop
    [/CODE]

    This made the page wait for exactly 5 seconds before continuing with processing ASP commands.
    However, I'd still be very interested if anyone can provide a Javascript solution to the problem.

    medicineworker

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      You may be looking for script.aculo.us.

      Comment

      • JamieHowarth0
        Recognized Expert Contributor
        • May 2007
        • 537

        #4
        Originally posted by acoder
        You may be looking for script.aculo.us.
        Hi acoder,

        Thanks for the link, but I'm not looking to use a JS library - I want to have control over my code and not be reliant upon third-party components to produce the functionality I require for (what should be) a relatively simple requirement.
        Is there not just a simple way in JS of setting the AJAX stateChanged functions to be called after a pre-defined time delay?

        Many thanks,

        medicineworker

        Comment

        Working...