472,975 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

AJAX setTimeout

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. }
  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
  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. }
  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. }
  54. function showContent(myAJ, myCont, myWait) {
  55.     myWait.style.visibility='hidden';
  56.     myCont.innerHTML = myAJ.responseText;
  57. }
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,

Sep 14 '07 #1
3 5399
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
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.

Sep 14 '07 #2
16,027 Expert Mod 8TB
You may be looking for script.aculo.us.
Sep 14 '07 #3
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,

Sep 21 '07 #4

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

Similar topics

by: Steve-O | last post by:
The following code works great in FireFox, Opera, Netscape, Safari, and Gecko, but NOT IE. Why? I tried using 'native' js with setInterval and setTimeout, but I get the same result. My IE...
by: quill | last post by:
Hi I am making a chatroom script and it appears that the problem seems to be that my setTimeout's are conflicting. The logic is as follows: Run a login check every x seconds Run a trigger...
by: Nico VanHaaster | last post by:
Hello all, I have run across an issue with IE 6.0+. I have a page that makes an XMLHttpRequest to the webserver to update a report on the page. The first time you hit the refresh report button...
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
by: daokfella | last post by:
I have a Webmethod in a server control which needs to be called using AJAX. The control writes a javascript block that uses setTimeout to call a an ajax js function after 10 seconds. When...
by: mndprasad | last post by:
Hi friends, Am new to AJAX coding, In my program am going to have two texbox which going to implent AJAX from same table. One box is going to retrieve the value of other and vice...
by: lak | last post by:
Hi friends, I have to parse a XML file and when it is changed I need to change in the front end. If I use the settimeout() then it is working. But I don't what to use the settimeout() function. ...
by: Bryan A | last post by:
Is there a way to add a timeout to this script so that it times out at a certain time. So it would be auto updating every 2seconds and it would timeout like after 100 seconds with a message?. ...
by: petvampire | last post by:
not sure if some one got a answer to this little issue. Im built a page that calls information from a page called database-update.asp and puts it in to the div tag Reload this. What i am trying to do...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.