473,666 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trouble with a timer (setInterval)

6 New Member
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 2461
kaiser0427
6 New Member
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 Recognized Expert Moderator MVP
If you want to display each business at 3 second intervals, keep the setInterval call outside displayBusiness es(), or use setTimeout if you use it within the function.
Jan 9 '09 #3
kaiser0427
6 New Member
@acoder
right. But if you look at my second piece of code it's not in the displayBusiness es(). I've tried placing it in numerous places. If I place it in the getData()... I get undefined variables inside the displayBusiness es(). 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 Recognized Expert Moderator MVP
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
kaiser0427
6 New Member
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
kaiser0427
6 New Member
thanks for your help. This issue is resolved.
Jan 16 '09 #7
acoder
16,027 Recognized Expert Moderator MVP
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
16102
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 http://weston.canncentral.org/misc/tkeep/tkeep.jss function fieldToClock(fieldId) { var field = document.getElementById(fieldId); alert("Starting a clock in text field " + fieldId + "(" + field
4
4382
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 takes an extra 35 seconds, but has taken an extra 2.5 minutes in some cases. Any ideas what might be the cause? And more importantly - the fix? Code snippet below... Thanks, Christine
3
1693
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
15207
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 am doing wrong? Your help will be much appreciated. Thanks
4
3250
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 the window.setInterval will not execute - there is no message but the timer never takes off. I put an alert in the startit() and it did appear so the attributes.add is working ok. Would appreciate any help thanks Private Sub...
13
1486
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 time, and carry on incrementing it if they click start again. Any suggestions on code, or reference material for this? Thanks for all the help
10
2450
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 additional user interaction. I found some timer stuff in js but it had to do with delays and not what I was really looking for. Thank you in advanced.
4
4526
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: http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_infinite If i click the 'start count' button once, it works okay. But when i click it twice or more, it just getting faster. Why is this? Does this mean the setTimeout in javascript will go faster if the...
3
11193
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); }
0
8449
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8360
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8784
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8556
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7387
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1777
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.