473,378 Members | 1,492 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,378 software developers and data experts.

trouble with a timer (setInterval)

Ok, I have cleaned up the code and fixed some errors in it. The problem I am having is with the timer. If you uncomment the alert it pauses after every record. when I replace it with setInterval it runs through the code but doesn't pause. First time using setInterval and any help will be appreciated.

// in the head

Expand|Select|Wrap|Line Numbers
  1. var c=0;
  2. var t;
  3. function wait() {
  4.     document.getElementById('targetDiv').value=c;
  5.     c+=1;
  6. }
  7. function getData() {
  8.      var mozillaFlag = false;
  9.      var XMLHttpRequestObject = false;
  10.  
  11.      if (window.XMLHttpRequest) {
  12.          XMLHttpRequestObject = new XMLHttpRequest();
  13.         mozillaFlag = true;
  14.         } 
  15.      else if (window.ActiveXObject) {
  16.          XMLHttpRequestObject = new
  17.             ActiveXObject ("Microsoft.XMLHTTP");
  18.         }    
  19.  
  20.      if (XMLHttpRequestObject) {
  21.          XMLHttpRequestObject.open("GET", "xmlData/business.xml", true);
  22.  
  23.         XMLHttpRequestObject.onreadystatechange = function() {
  24.             if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
  25.                 var xmlDocument = XMLHttpRequestObject.responseXML;
  26.                 if(mozillaFlag) {
  27.                     removeWhitespace(xmlDocument);
  28.                 }
  29.                 displayBusinesses(xmlDocument);
  30.             }
  31.           }
  32.         XMLHttpRequestObject.send(null);
  33.      }
  34.     //wait();
  35. }
  36.  
  37. function displayBusinesses(xmldoc) {
  38.  
  39.     var businessesNode, WbusinessNode, featuredNode, logoNode, nameNode, descriptionNode, contactNode;
  40.     businessesNode = xmldoc.getElementsByTagName("businesses");
  41.     WbusinessNode = xmldoc.getElementsByTagName("Fbusiness");
  42.     featuredNode = xmldoc.getElementsByTagName("featured");
  43.     logoNode = xmldoc.getElementsByTagName("logo");
  44.     nameNode = xmldoc.getElementsByTagName("name");
  45.     descriptionNode = xmldoc.getElementsByTagName("description");
  46.     contactNode = xmldoc.getElementsByTagName("contact");
  47.     var imagesFront = "<img src=";
  48.     var imagesBack = " />";
  49.     var linebreak = "\<br>";
  50.  
  51.     var counter;    
  52.     var temp= xmldoc.getElementsByTagName('featured');
  53.     var temp2 = document.getElementById("vars");
  54.     temp2.innerHTML="# of nodes in featured is: " + temp.length;
  55.  
  56.     for (counter=0; counter < temp.length; counter++) {
  57.         //clearInterval(t);
  58.         //if (temp.length != last node of xml then execute code else if (temp.length = last node then go to first node.
  59.         featuredBusinesses = nameNode[counter].firstChild.nodeValue + linebreak + linebreak + imagesFront + logoNode[counter].firstChild.nodeValue + imagesBack;
  60.         featuredBusinesses1 = descriptionNode[counter].firstChild.nodeValue + contactNode[counter].firstChild.nodeValue;
  61.          var target = document.getElementById("targetDiv");
  62.         target.innerHTML=featuredBusinesses + featuredBusinesses1 + counter + "this is the value for t: " + t;
  63.         //alert("you are node number: " + counter);  // when you uncomment this line it pauses after every record displayed but now with setInterval
  64.         //alert("t =: " + t);
  65.         t=window.setInterval('displayBusinesses()', 3000);
  66.         //alert("t =: " + t);
  67.     }
  68. }
  69.  
  70. function removeWhitespace(xml) {
  71.     var loopIndex;
  72.     for (loopIndex = 0; loopIndex < xml.childNodes.length;
  73.         loopIndex++) {
  74.  
  75.         var currentNode = xml.childNodes[loopIndex];
  76.  
  77.         if (currentNode.nodeType == 1) {
  78.         removeWhitespace(currentNode);
  79.  
  80.         if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType == 3)) {
  81.         xml.removeChild(xml.childNodes[loopIndex--]);
  82.         }
  83.       }
  84.     }
  85. }
  86.  
working example is at:
http://davispubs.com/westbycoc/xmltest.html
Jan 8 '09 #1
7 2450
updated code that almost works correctly. I can't figure out where to put the call for the wait() function. The way it is now in this example. It will loop back when it gets to the end of the nodes and start over at the beginning, however, it ONLY pauses at the alert between each business displayed. Of course, I don't want anyone to have to hit ok to see the next business. Where am I going wrong?

Expand|Select|Wrap|Line Numbers
  1. var intervalID;
  2. function wait() {
  3.    intervalID = setInterval('getData()', 3000);  
  4. }
  5. function getData() {
  6.      var mozillaFlag = false;
  7.      var XMLHttpRequestObject = false;
  8.  
  9.      if (window.XMLHttpRequest) {
  10.          XMLHttpRequestObject = new XMLHttpRequest();
  11.         mozillaFlag = true;
  12.         } 
  13.      else if (window.ActiveXObject) {
  14.          XMLHttpRequestObject = new
  15.             ActiveXObject ("Microsoft.XMLHTTP");
  16.         }    
  17.  
  18.      if (XMLHttpRequestObject) {
  19.          XMLHttpRequestObject.open("GET", "xmlData/business.xml", true);
  20.  
  21.         XMLHttpRequestObject.onreadystatechange = function() {
  22.             if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
  23.                 var xmlDocument = XMLHttpRequestObject.responseXML;
  24.                 if(mozillaFlag) {
  25.                     removeWhitespace(xmlDocument);
  26.                 }
  27.                 displayBusinesses(xmlDocument);
  28.             }
  29.           }
  30.         XMLHttpRequestObject.send(null);
  31.      }
  32. }
  33.  
  34. function displayBusinesses(xmldoc) {
  35.  
  36.     var businessesNode, WbusinessNode, featuredNode, logoNode, nameNode, descriptionNode, contactNode;
  37.     businessesNode = xmldoc.getElementsByTagName("businesses");
  38.     WbusinessNode = xmldoc.getElementsByTagName("Fbusiness");
  39.     featuredNode = xmldoc.getElementsByTagName("featured");
  40.     logoNode = xmldoc.getElementsByTagName("logo");
  41.     nameNode = xmldoc.getElementsByTagName("name");
  42.     descriptionNode = xmldoc.getElementsByTagName("description");
  43.     contactNode = xmldoc.getElementsByTagName("contact");
  44.     var imagesFront = "<img src=";
  45.     var imagesBack = " />";
  46.     var linebreak = "\<br>";
  47.  
  48.     var counter;    
  49.     var temp2 = document.getElementById("vars");
  50.     temp2.innerHTML="# of nodes in featured is: " + featuredNode.length;
  51.  
  52.  
  53.     for (counter=0; counter < featuredNode.length; counter++) {
  54.         //clearInterval(intervalID);
  55.         //if (featuredNode.length != featuredNode.LastChild) {
  56.  
  57.         //if (featuredNode.length != last node of xml then execute code else if (temp.length = last node then go to first node.
  58.         featuredBusinesses = nameNode[counter].firstChild.nodeValue + linebreak + linebreak + imagesFront + logoNode[counter].firstChild.nodeValue + imagesBack;
  59.         featuredBusinesses1 = descriptionNode[counter].firstChild.nodeValue + contactNode[counter].firstChild.nodeValue;
  60.          var target = document.getElementById("targetDiv");
  61.         target.innerHTML=featuredBusinesses + featuredBusinesses1 + counter + "&mdash; this is the value for intervalID: " + intervalID;
  62.         //alert("you are node number: " + counter);
  63.         //}
  64.         //else {
  65.         //featuredNode = featuredNode.firstChild.nodeValue;
  66.         alert(featuredNode.value);
  67.         //}
  68.     }
  69. }
  70.  
  71. function removeWhitespace(xml) {
  72.     var loopIndex;
  73.     for (loopIndex = 0; loopIndex < xml.childNodes.length;
  74.         loopIndex++) {
  75.  
  76.         var currentNode = xml.childNodes[loopIndex];
  77.  
  78.         if (currentNode.nodeType == 1) {
  79.         removeWhitespace(currentNode);
  80.  
  81.         if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType == 3)) {
  82.         xml.removeChild(xml.childNodes[loopIndex--]);
  83.         }
  84.       }
  85.     }
  86. }
  87. </script>
</head>
[HTML]
<body onload="wait();">[/HTML]

http://davispubs.com/westbycoc/xmltest_bytes.html
Jan 8 '09 #2
acoder
16,027 Expert Mod 8TB
If you want to display each business at 3 second intervals, keep the setInterval call outside displayBusinesses(), or use setTimeout if you use it within the function.
Jan 9 '09 #3
@acoder
right. But if you look at my second piece of code it's not in the displayBusinesses(). I've tried placing it in numerous places. If I place it in the getData()... I get undefined variables inside the displayBusinesses(). I know it's probably a simple fix and I will kick myself when the problem is discovered.
Jan 11 '09 #4
acoder
16,027 Expert Mod 8TB
That may be so, but the first version is closer to what you're looking for. Get all the data and then when displaying, use setInterval to call a function to display the next business every 3 seconds.
Jan 12 '09 #5
the first version is closer to what you're looking for.
thanks. that gives me a direction in which to go.
Jan 12 '09 #6
thanks for your help. This issue is resolved.
Jan 16 '09 #7
acoder
16,027 Expert Mod 8TB
Glad to hear it. Until next time...
Jan 18 '09 #8

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

Similar topics

1
by: Weston C | last post by:
In the course of trying to build a simple clock, I've run into a problem using the setInterval (and setTimeout) function. http://weston.canncentral.org/misc/tkeep/tkeep.html...
4
by: Christine | last post by:
I've implemented a countdown timer for a tutorial web page that should give the user 45 minutes to complete the test, only to find that the timer is slowly 'losing' time. On average, it actually...
3
by: Richie | last post by:
Is there a way to access an Event object associated with setInterval()? I'm using Mozilla and Firefox on various platforms.
6
by: marktm | last post by:
Hi- I am trying to use setInterval to call a method within an object and have not had any luck. I get "Object doesn't support this property or method" when I execute the following code. What I...
4
by: barry | last post by:
If The script below is exeuted from the onload="startit()" in the body tag everything works fine but when I remove the onload and try to execute by using the attributes.add from within the Page_Load...
13
by: David | last post by:
Hi, Im not sure where to find all the documentation i need for this? I need to timer since a start button has been pushed, and show a counter on a page. If they click stop i want to keep the...
10
by: jason_box | last post by:
Hello, I was wondering if there was a way to have a javacript be activated by an input button that would call to a cgi program and querey every 10minutes and the cgi would update the page without...
4
by: agun | last post by:
Hello, Hi, i'm really a newbie in programming, especially javascript. I have one question in my mind right now. Sorry before if i'm not clear. Please see this example: ...
3
by: Lagon666 | last post by:
When i run this functions it says the count is undefined. function hello(){ var count = 0; var timer = setInterval('count+=1;alert(count);',2000); }
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.