473,473 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

javascript setTimeout()

21 New Member
i'd like to know what set timeout does after its finished. I have a script that starts a timeout right before sending an SJaX request that timesout after 1000 milliseconds and abort()s the SJaX request and sets some things then alerts me that it timed out. After i click ok on the alert, my browser (FF) crashes every time. I'd just like to know what happens after the timeout is finished. Here is my code:
Expand|Select|Wrap|Line Numbers
  1. var changeBG;
  2. function fetchById(id){
  3.  
  4.     if (document.getElementById){
  5.  
  6.         var fetch = document.getElementById(id);
  7.  
  8.     }
  9.  
  10.     else if (document.layers){
  11.  
  12.         var fetch = document.layers[id];
  13.  
  14.     }
  15.  
  16.     else if (document.all){
  17.  
  18.         var fetch = document.all[id];
  19.  
  20.     }
  21.  
  22.     return fetch;
  23.  
  24. }
  25. function SJAX(url){
  26.     http_request = false;
  27.     if(window.XMLHttpRequest){
  28.         // Mozilla - Safari - Opera
  29.         http_request = new XMLHttpRequest();
  30.     }
  31.     else if(window.ActiveXObject){
  32.         // Internet Explorer
  33.         try{
  34.             http_request = new ActiveXObject("Msxml2.XMLHTTP");
  35.         }
  36.         catch(e){
  37.             try{
  38.                 http_request = new ActiveXObject("Microsoft.XMLHTTP");
  39.             }
  40.             catch(e){
  41.                 //don't do anything and let !http_request do it's job
  42.             }
  43.         }
  44.     }
  45.     if(!http_request){
  46.         updateData('Error: Cannot create an XMLHttpRequest instance!');
  47.         return false;
  48.     }
  49.     http_request.open('GET',url, false);
  50.     timeout = setTimeout("http_request.abort(); alert('Error: Request timed out!'); timerRunning = false;",1000);
  51.     timerRunning = true;
  52.     http_request.send(null);
  53.     if(timerRunning == true){
  54.         clearTimeout(timeout);
  55.     }
  56.  
  57.     if(http_request.status == 200){
  58.         if(http_request.readyState == 4){
  59.             var recoil = http_request.responseText;
  60.             if(recoil == "0"){
  61.                 alert("DCL " + validatingDCL + " is invalid.");
  62.             }
  63.             else{
  64.                 return recoil;
  65.             }
  66.         }
  67.     }
  68. }
  69. function runTest(){
  70.     for(i=0;i<document.options.testType.length;i++){
  71.         if(document.options.testType[i].checked==true){
  72.             testType = i;
  73.         }
  74.     }
  75. //    speed = fetchById('speed').checked;
  76.     if(testType == 1){
  77.         timeSpanFull = fetchById('timeSpan').value;
  78.         timeSpan = timeSpanFull.split(".");
  79.         timeSpan[0] = parseInt(timeSpan[0]);
  80.         timeSpan[1] = parseInt(timeSpan[1]);
  81.         if(timeSpan[1] == undefined || isNaN(timeSpan[1])){
  82.             timeSpan[1] = 0;
  83.         }
  84.         if(timeSpan[0] == undefined || isNaN(timeSpan[0])){
  85.             timeSpan[0] = 0;
  86.         }
  87.         good = true;
  88.         updateData('', true);
  89.         x = 0;
  90.         beginTime = new Date();
  91.         trueBeginTime = beginTime;
  92.         updateData("<div style='background: #eee; width: 100%; display: inline-table;'>Begin Time: " + beginTime.getMinutes() + ":" + beginTime.getSeconds() + ":" + beginTime.getMilliseconds() + "</div>",false,false);
  93.         beginTime.setSeconds(beginTime.getSeconds() + timeSpan[0]);
  94.         beginTime.setMilliseconds(beginTime.getMilliseconds() + (timeSpan[1] * 100));
  95.         while(good == true){
  96.             currentTime = new Date();
  97.             hours = currentTime.getHours();
  98.             minutes = currentTime.getMinutes();
  99.             seconds = currentTime.getSeconds();
  100.             milliseconds = currentTime.getMilliseconds();
  101.             if(currentTime > beginTime || x >= 500){
  102.                 good = false;
  103.                 break;
  104.             }
  105.             x = x + 1;
  106.             updateData(x + ": " + hours + ":" + minutes + ":" + seconds + ":" + milliseconds +" - " + SJAX('ajax.php'));
  107.         }
  108.         elapsedMinutes = minutes - trueBeginTime.getMinutes();
  109.         elapsedSeconds = seconds - trueBeginTime.getSeconds();
  110.         elapsedMilliseconds = milliseconds - trueBeginTime.getMilliseconds();
  111.         updateData("<div style='background: #eee; width: 100%; display: inline-table;'>End Time: " + minutes + ":" + seconds + ":" + milliseconds + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Total Time: " + elapsedMinutes + ":" + elapsedSeconds + ":" + elapsedMilliseconds + "</div>",false,false);
  112.     }
  113.     else if(testType == 2){
  114.         reqs = Math.ceil(parseInt(fetchById('timeSpan').value));
  115.         good = true;
  116.         updateData('', true);
  117.         x = 0;
  118.         beginTime = new Date();
  119.         trueBeginTime = beginTime;
  120.         updateData("<div style='background: #eee; width: 100%; display: inline-table;'>Begin Time: " + beginTime.getMinutes() + ":" + beginTime.getSeconds() + ":" + beginTime.getMilliseconds() + "</div>",false,false);
  121.         while(good == true){
  122.             currentTime = new Date();
  123.             hours = currentTime.getHours();
  124.             minutes = currentTime.getMinutes();
  125.             seconds = currentTime.getSeconds();
  126.             milliseconds = currentTime.getMilliseconds();
  127.             if(x >= reqs){
  128.                 good = false;
  129.                 break;
  130.             }
  131.             x = x + 1;
  132.             updateData(x + ": " + hours + ":" + minutes + ":" + seconds + ":" + milliseconds +" - " + SJAX('ajax.php'));
  133.         }
  134.         elapsedMinutes = minutes - trueBeginTime.getMinutes();
  135.         elapsedSeconds = seconds - trueBeginTime.getSeconds();
  136.         elapsedMilliseconds = milliseconds - trueBeginTime.getMilliseconds();
  137.         updateData("<div style='background: #eee; width: 100%; display: inline-table;'>End Time: " + minutes + ":" + seconds + ":" + milliseconds + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Total Time: " + elapsedMinutes + ":" + elapsedSeconds + ":" + elapsedMilliseconds + "</div>",false,false);
  138.     }
  139.     else if(testType == 3){
  140.         updateData(SJAX('timeout.php'),true);
  141.  
  142.     }
  143.     else{
  144.         updateData(SJAX('ajax.php'), true);
  145.     }
  146. }
  147. function updateData(data,clear,br){
  148.     if(clear == true){
  149.         //Clear the Bottom section
  150.         fetchById('bottom').innerHTML = "";
  151.     }
  152.     if(br == false){
  153.         //Don't add a line break after the line
  154.         br = "";
  155.     }
  156.     else{
  157.         br = "<br />"
  158.     }
  159.     //Populate the Bottom section with more data
  160.     fetchById('bottom').innerHTML = fetchById('bottom').innerHTML + data + br;
  161.     //Change Bottom sections bgcolor to a light yellow for notification
  162.     fetchById('bottom').style.backgroundColor = "#FFC";
  163.     if(data == ""){
  164.         //Clear the field so it doesn't have a break tag
  165.         fetchById('bottom').innerHTML = "";
  166.     }
  167.     //Clear the timeout so you don't have early background changes
  168.     clearTimeout(changeBG);
  169.     //Set the background to change back after 1 second
  170.     changeBG = setTimeout("fetchById('bottom').style.backgroundColor = '#FFF';", 1000);
  171. }
  172. function switchVal(elem){
  173.     if(fetchById(elem).checked == true){
  174.         fetchById(elem).checked = false;
  175.         fetchById(elem).focus();
  176.     }
  177.     else{
  178.         fetchById(elem).checked = true;
  179.         fetchById(elem).focus();
  180.     }
  181. }
Aug 1 '07 #1
3 5746
naurus
21 New Member
seriously, i need some help here. doesn't anybody have anything to say?
Aug 1 '07 #2
iam_clint
1,208 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1.  timeout = setTimeout("http_request.abort(); alert('Error: Request timed out!'); timerRunning = false;",1000);

Well its aborting the query, alerting you that the request has timed out then setting a variable to false;
Aug 1 '07 #3
naurus
21 New Member
Expand|Select|Wrap|Line Numbers
  1.  timeout = setTimeout("http_request.abort(); alert('Error: Request timed out!'); timerRunning = false;",1000);

Well its aborting the query, alerting you that the request has timed out then setting a variable to false;
true, but i need to know what happens afterwards, because whatever does is crashing my browser.
Aug 2 '07 #4

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

Similar topics

4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
10
by: Shadow Lynx | last post by:
That subject packs a whallop, so let me explain in better detail what's happening and how it relates to ASPX pages... In a nutshell, if the first <script /on a page is of type "text/vbscript",...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.