473,746 Members | 2,670 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;
  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.         }    
  20.      if (XMLHttpRequestObject) {
  21.          XMLHttpRequestObject.open("GET", "xmlData/business.xml", true);
  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. }
  37. function displayBusinesses(xmldoc) {
  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>";
  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;
  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. }
  70. function removeWhitespace(xml) {
  71.     var loopIndex;
  72.     for (loopIndex = 0; loopIndex < xml.childNodes.length;
  73.         loopIndex++) {
  75.         var currentNode = xml.childNodes[loopIndex];
  77.         if (currentNode.nodeType == 1) {
  78.         removeWhitespace(currentNode);
  80.         if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType == 3)) {
  81.         xml.removeChild(xml.childNodes[loopIndex--]);
  82.         }
  83.       }
  84.     }
  85. }
working example is at:
Jan 8 '09 #1
7 2469
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;
  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.         }    
  18.      if (XMLHttpRequestObject) {
  19.          XMLHttpRequestObject.open("GET", "xmlData/business.xml", true);
  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. }
  34. function displayBusinesses(xmldoc) {
  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>";
  48.     var counter;    
  49.     var temp2 = document.getElementById("vars");
  50.     temp2.innerHTML="# of nodes in featured is: " + featuredNode.length;
  53.     for (counter=0; counter < featuredNode.length; counter++) {
  54.         //clearInterval(intervalID);
  55.         //if (featuredNode.length != featuredNode.LastChild) {
  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. }
  71. function removeWhitespace(xml) {
  72.     var loopIndex;
  73.     for (loopIndex = 0; loopIndex < xml.childNodes.length;
  74.         loopIndex++) {
  76.         var currentNode = xml.childNodes[loopIndex];
  78.         if (currentNode.nodeType == 1) {
  79.         removeWhitespace(currentNode);
  81.         if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType == 3)) {
  82.         xml.removeChild(xml.childNodes[loopIndex--]);
  83.         }
  84.       }
  85.     }
  86. }
  87. </script>
<body onload="wait(); ">[/HTML]

Jan 8 '09 #2
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
6 New Member
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
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
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
6 New Member
thanks for your help. This issue is resolved.
Jan 16 '09 #7
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

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
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
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.
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
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...
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
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.
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...
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); }
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...
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,...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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,...
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...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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.