473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

sleep() || settimeout()

I`m writing script that makes HEAD request to multiple web sites and *multiple* is the problem.
If i`m using
xmlhttp.open("HEAD", url,true) script shows actual status only for last array object, but if i use false script end after first unsuccessful request.
Does anyone have any idea how to workaround that problem or make script better.
Thanks!

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="javascript">
  2. var xmlhttp=false;
  3. /*@if (@_jscript_version >= 5)
  4.  try {
  5.   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  6.  } catch (e) {
  7.   try {
  8.    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  9.   } catch (E) {
  10.    xmlhttp = false;
  11.   }
  12.  }
  13. @end @*/
  14. if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  15.     try {
  16.         xmlhttp = new XMLHttpRequest();
  17.     } catch (e) {
  18.         xmlhttp=false;
  19.     }
  20. }
  21. if (!xmlhttp && window.createRequest) {
  22.     try {
  23.         xmlhttp = window.createRequest();
  24.     } catch (e) {
  25.         xmlhttp=false;
  26.     }
  27. }
  28.  
  29. function larray() {
  30. var arr = new Array();
  31. arr["1"] = "www.google.com";
  32. arr["2"] = "www.kernel.org";
  33. arr["3"] = "xxx";
  34. arr["4"] = "yyy";
  35. for(var i in arr){
  36. getans(arr[i],i);
  37. }}
  38.  
  39. function getans(url,i) {
  40. xmlhttp.open("HEAD", url,true);
  41.  xmlhttp.onreadystatechange=function() {
  42.   if (xmlhttp.readyState!=4) {
  43.     document.getElementById('l'+i).innerHTML="Processing..."
  44.   }
  45.    if (xmlhttp.readyState==4) {
  46.    if (xmlhttp.status==200)
  47.    {
  48.     document.getElementById('l'+i).innerHTML="Ok!"
  49.   }
  50.     else if (xmlhttp.status==404)
  51.     {
  52.      document.getElementById('l'+i).innerHTML="Not ok!"
  53.      }
  54.      else
  55.      {
  56.       document.getElementById('l'+i).innerHTML="Problem"
  57.      }
  58.   }
  59. }
  60.  xmlhttp.send(null)
  61.  }
  62. </script>
  63. <body onload="larray()">
  64. <p><b>1:</b> <span id="l1"></span></p>
  65. <p><b>2:</b> <span id="l2"></span></p>
  66. <p><b>3:</b> <span id="l3"></span></p>
  67. <p><b>4:</b> <span id="l4"></span></p>
  68.  
Feb 14 '07 #1
4 3609
acoder
16,027 Expert Mod 8TB
true means asynchronous while false means synchronous, so what you've described is correct.

Maybe you need to create a new HTTPRequest. Put the code into a function and create a new HTTP request for each array item.
Feb 14 '07 #2
Yes thanks but i found that out by my self yesterday. Make request for each array item works fine but in synchronous mode only.
Now i`m dealing with other problems, like best way to parse array elements for request and script works on locale machine fine but when i put script on server and accessing it script ends with error.
Feb 15 '07 #3
acoder
16,027 Expert Mod 8TB
Post your code.
Feb 15 '07 #4
Only few html tags support onload method. Those are:
Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <frame>
  3. <frameset>
  4. <iframe>
  5. <img>
  6. <link>
  7. <script>
  8.  
I decide to use img tag:
Expand|Select|Wrap|Line Numbers
  1. <p><img src="20.gif" onload="getans('http://kernel.org',2)"><b>2:</b> <span id="l2"></span></p>
All code:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="javascript">
  2. var xmlhttp=false;
  3. /*@if (@_jscript_version >= 5)
  4.  try {
  5.   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  6.  } catch (e) {
  7.   try {
  8.    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  9.   } catch (E) {
  10.    xmlhttp = false;
  11.   }
  12.  }
  13. @end @*/
  14. if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  15.     try {
  16.         xmlhttp = new XMLHttpRequest();
  17.     } catch (e) {
  18.         xmlhttp=false;
  19.     }
  20. }
  21. if (!xmlhttp && window.createRequest) {
  22.     try {
  23.         xmlhttp = window.createRequest();
  24.     } catch (e) {
  25.         xmlhttp=false;
  26.     }
  27. }
  28.  
  29. function getans(url,i) {
  30. xmlhttp.open("HEAD", url,false);
  31.  xmlhttp.onreadystatechange=function() {
  32.   if (xmlhttp.readyState!=4) {
  33.     document.getElementById('l'+i).innerHTML="<img src='loading.gif'>"
  34.   }
  35.    if (xmlhttp.readyState==4) {
  36.    if (xmlhttp.status==200)
  37.    {
  38.     document.getElementById('l'+i).innerHTML="<img src='answer_good.gif'>"
  39.   }
  40.     else if (xmlhttp.status==404)
  41.     {
  42.      document.getElementById('l'+i).innerHTML="<img src='answer_bad.gif'>"
  43.      }
  44.      else
  45.      {
  46.       document.getElementById('l'+i).innerHTML="<img src='answer_bad.gif'>"
  47.      }
  48.   }
  49. }
  50.  xmlhttp.send(null)
  51.  }
  52. </script>
  53. <body>
  54. <p><img src="20.gif" onload="getans('yyy',1)"><b>1:</b> <span id="l1"></span></p>
  55. <p><img src="20.gif" onload="getans('http://kernel.org',2)"><b>2:</b> <span id="l2"></span></p>
  56. <p><img src="20.gif" onload="getans('http://google.com',3)"><b>3:</b> <span id="l3"></span></p>
  57. <p><img src="20.gif" onload="getans('xxx',4)"><b>4:</b> <span id="l4"></span></p>
  58. </body>
  59.  
On my machine code works great, but due browser security and other things script does not work on other machines and other browsers thats why i decide to write small program in java instead of using this script and dealing with all those things why script isn`t working.
Feb 19 '07 #5

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

Similar topics

2
by: JW | last post by:
I wanted have this as part of a flood control script: <? echo ("Flood control in place - please wait " . $floodinterval . " seconds between postings."); sleep(5); // go back two pages echo...
3
by: K Balusu | last post by:
Hi all, We have to develop a small engine which the client uses. It supposed to work like this. Our engine resides in a frame (frameA) which will be loaded only once and it provides set of...
4
by: Xaradas | last post by:
How can I suspend the execution of a script for x seconds? Does exists something like sleep(x)? Thanks. ------------------------------------ "Computer Science is no more about computers ...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
4
by: coolsti | last post by:
I am aware of the setInterval and setTimeout functions for Javascript, but I can't seem to find an example that lets me do what I need to. I have a script that needs to wait until a certain...
4
by: Alexander Erlich | last post by:
Hello, I have written a function that returns a random number in dependence of the time running on the current system. The problem is that initializing two variables with this function _one...
6
by: M B HONG 20 | last post by:
Hi all - I'm trying to emulate a sleep function in javascript. I know of this method for example: window.setTimeout("function1();", 500); However, I was wondering if there was a way to...
6
by: Brian | last post by:
Hello, Is there any way to make a web application that will refresh itself after a certain amount of time? I am trying to make a messageing program that will refresh and grab the new messages...
10
by: VK | last post by:
Yet about the code posted at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/bf33c7bbb9f699f9/0336cb59fb5920c5> I just noticed another thing I did not notice last year...
2
by: fltcpt | last post by:
After reading the many posts on the newsgroup, I still disagree with the people who claim you will never need a sleep(), or that a sleep() is a bad idea, or that sleep() does not fit in the event...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.