471,093 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,093 software developers and data experts.

AJAX setTimeout

JamieHowarth0
533 Expert 512MB
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.

Expand|Select|Wrap|Line Numbers
  1. function GetXmlHttpObject() {
  2. var xmlHttp=null;
  3. try {
  4.     // Firefox, Opera 8.0+, Safari
  5.     xmlHttp=new XMLHttpRequest();
  6. } catch (e) {
  7.     // Internet Explorer
  8.     try {
  9.         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  10.     } catch (e) {
  11.         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  12.     }
  13. }
  14. return xmlHttp;
  15. }
  16.  
  17. var myBodyAjax = new GetXmlHttpObject();
  18. var myOCAjax = new GetXmlHttpObject();
  19. var myURL, myParams, myMethod, myContainer, myWaitingContainer, myAJObject
  20. var thisAJ, thisCont, thisWaitCont
  21. var myAJ, myCont, myWait
  22.  
  23. function doAjax(myURL, myParams, myMethod, myContainer, myWaitingContainer, myAJObject) {
  24. if (myAJObject==null) {
  25.     alert ("Your browser does not support AJAX!");
  26.     return;
  27. }
  28. myAJObject.onreadystatechange=function(){stateChanged(myAJObject, myContainer, myWaitingContainer)};
  29. switch (myMethod) {
  30.     case "get":
  31.         myAJObject.open("GET",myURL+"?"+myParams,true);
  32.         myAJObject.send(null);
  33.         break;
  34.     case "post":
  35.         myAJObject.open("POST",myURL,true);
  36.         myAJObject.send(myParams);
  37.         break;
  38.     default:
  39.         alert("AJAX data method not specified - please email webmaster@cmdev-associates.com");
  40. }
  41. myAJObject.close;
  42. }
  43.  
  44. function stateChanged(thisAJ, thisCont, thisWaitCont) {
  45. switch (thisAJ.readyState) {
  46.     case 4:
  47.         var timer = setTimeout('function(){showContent(thisAJ,thisCont,thisWaitCont)}',2000);
  48.         break;
  49.     default:
  50.         thisWaitCont.style.visibility = "visible";
  51. }
  52. }
  53.  
  54. function showContent(myAJ, myCont, myWait) {
  55.     myWait.style.visibility='hidden';
  56.     myCont.innerHTML = myAJ.responseText;
  57. }
  58.  
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
Sep 14 '07 #1
3 5271
JamieHowarth0
533 Expert 512MB
Hi all,

I solved the problem myself! I added a section of ASP code to my AJAX content-serving page:
Expand|Select|Wrap|Line Numbers
  1. Dim startTime
  2. startTime = Now()
  3. Do While DateDiff("s",startTime,Now()) < 5
  4.        'Do absolutely nothing!
  5. Loop
  6.  
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
Sep 14 '07 #2
acoder
16,027 Expert Mod 8TB
You may be looking for script.aculo.us.
Sep 14 '07 #3
JamieHowarth0
533 Expert 512MB
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
Sep 21 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

17 posts views Thread by Steve-O | last post: by
6 posts views Thread by Nico VanHaaster | last post: by
6 posts views Thread by =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post: by
4 posts views Thread by lak | last post: by
20 posts views Thread by Bryan A | last post: by
1 post views Thread by petvampire | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.