trouble with a timer (setInterval)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kaiser0427
    New Member
    • Jan 2009
    • 6

    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

    Code:
    var c=0;
    var t;
    function wait() {
        document.getElementById('targetDiv').value=c;
        c+=1;
    }
    function getData() {
         var mozillaFlag = false;
         var XMLHttpRequestObject = false;
         
         if (window.XMLHttpRequest) {
             XMLHttpRequestObject = new XMLHttpRequest();
            mozillaFlag = true;
            } 
         else if (window.ActiveXObject) {
             XMLHttpRequestObject = new
                ActiveXObject ("Microsoft.XMLHTTP");
            }    
         
         if (XMLHttpRequestObject) {
             XMLHttpRequestObject.open("GET", "xmlData/business.xml", true);
        
            XMLHttpRequestObject.onreadystatechange = function() {
                if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                    var xmlDocument = XMLHttpRequestObject.responseXML;
                    if(mozillaFlag) {
                        removeWhitespace(xmlDocument);
                    }
                    displayBusinesses(xmlDocument);
                }
              }
            XMLHttpRequestObject.send(null);
         }
        //wait();
    }
    
    function displayBusinesses(xmldoc) {
    
        var businessesNode, WbusinessNode, featuredNode, logoNode, nameNode, descriptionNode, contactNode;
        businessesNode = xmldoc.getElementsByTagName("businesses");
        WbusinessNode = xmldoc.getElementsByTagName("Fbusiness");
        featuredNode = xmldoc.getElementsByTagName("featured");
        logoNode = xmldoc.getElementsByTagName("logo");
        nameNode = xmldoc.getElementsByTagName("name");
        descriptionNode = xmldoc.getElementsByTagName("description");
        contactNode = xmldoc.getElementsByTagName("contact");
        var imagesFront = "<img src=";
        var imagesBack = " />";
        var linebreak = "\<br>";
        
        var counter;    
        var temp= xmldoc.getElementsByTagName('featured');
        var temp2 = document.getElementById("vars");
        temp2.innerHTML="# of nodes in featured is: " + temp.length;
    
        for (counter=0; counter < temp.length; counter++) {
            //clearInterval(t);
            //if (temp.length != last node of xml then execute code else if (temp.length = last node then go to first node.
            featuredBusinesses = nameNode[counter].firstChild.nodeValue + linebreak + linebreak + imagesFront + logoNode[counter].firstChild.nodeValue + imagesBack;
            featuredBusinesses1 = descriptionNode[counter].firstChild.nodeValue + contactNode[counter].firstChild.nodeValue;
             var target = document.getElementById("targetDiv");
            target.innerHTML=featuredBusinesses + featuredBusinesses1 + counter + "this is the value for t: " + t;
            //alert("you are node number: " + counter);  // when you uncomment this line it pauses after every record displayed but now with setInterval
            //alert("t =: " + t);
            t=window.setInterval('displayBusinesses()', 3000);
            //alert("t =: " + t);
        }
    }
    
    function removeWhitespace(xml) {
        var loopIndex;
        for (loopIndex = 0; loopIndex < xml.childNodes.length;
            loopIndex++) {
            
            var currentNode = xml.childNodes[loopIndex];
            
            if (currentNode.nodeType == 1) {
            removeWhitespace(currentNode);
            
            if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType == 3)) {
            xml.removeChild(xml.childNodes[loopIndex--]);
            }
          }
        }
    }
    working example is at:
    Last edited by kaiser0427; Jan 8 '09, 03:55 PM. Reason: adding a url
  • kaiser0427
    New Member
    • Jan 2009
    • 6

    #2
    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?

    Code:
    var intervalID;
    function wait() {
       intervalID = setInterval('getData()', 3000);  
    }
    function getData() {
         var mozillaFlag = false;
         var XMLHttpRequestObject = false;
         
         if (window.XMLHttpRequest) {
             XMLHttpRequestObject = new XMLHttpRequest();
            mozillaFlag = true;
            } 
         else if (window.ActiveXObject) {
             XMLHttpRequestObject = new
                ActiveXObject ("Microsoft.XMLHTTP");
            }    
         
         if (XMLHttpRequestObject) {
             XMLHttpRequestObject.open("GET", "xmlData/business.xml", true);
        
            XMLHttpRequestObject.onreadystatechange = function() {
                if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
                    var xmlDocument = XMLHttpRequestObject.responseXML;
                    if(mozillaFlag) {
                        removeWhitespace(xmlDocument);
                    }
                    displayBusinesses(xmlDocument);
                }
              }
            XMLHttpRequestObject.send(null);
         }
    }
    
    function displayBusinesses(xmldoc) {
    
        var businessesNode, WbusinessNode, featuredNode, logoNode, nameNode, descriptionNode, contactNode;
        businessesNode = xmldoc.getElementsByTagName("businesses");
        WbusinessNode = xmldoc.getElementsByTagName("Fbusiness");
        featuredNode = xmldoc.getElementsByTagName("featured");
        logoNode = xmldoc.getElementsByTagName("logo");
        nameNode = xmldoc.getElementsByTagName("name");
        descriptionNode = xmldoc.getElementsByTagName("description");
        contactNode = xmldoc.getElementsByTagName("contact");
        var imagesFront = "<img src=";
        var imagesBack = " />";
        var linebreak = "\<br>";
        
        var counter;    
        var temp2 = document.getElementById("vars");
        temp2.innerHTML="# of nodes in featured is: " + featuredNode.length;
        
        
        for (counter=0; counter < featuredNode.length; counter++) {
            //clearInterval(intervalID);
            //if (featuredNode.length != featuredNode.LastChild) {
            
            //if (featuredNode.length != last node of xml then execute code else if (temp.length = last node then go to first node.
            featuredBusinesses = nameNode[counter].firstChild.nodeValue + linebreak + linebreak + imagesFront + logoNode[counter].firstChild.nodeValue + imagesBack;
            featuredBusinesses1 = descriptionNode[counter].firstChild.nodeValue + contactNode[counter].firstChild.nodeValue;
             var target = document.getElementById("targetDiv");
            target.innerHTML=featuredBusinesses + featuredBusinesses1 + counter + "&mdash; this is the value for intervalID: " + intervalID;
            //alert("you are node number: " + counter);
            //}
            //else {
            //featuredNode = featuredNode.firstChild.nodeValue;
            alert(featuredNode.value);
            //}
        }
    }
    
    function removeWhitespace(xml) {
        var loopIndex;
        for (loopIndex = 0; loopIndex < xml.childNodes.length;
            loopIndex++) {
            
            var currentNode = xml.childNodes[loopIndex];
            
            if (currentNode.nodeType == 1) {
            removeWhitespace(currentNode);
            
            if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType == 3)) {
            xml.removeChild(xml.childNodes[loopIndex--]);
            }
          }
        }
    }
    </script>
    </head>
    [HTML]
    <body onload="wait(); ">[/HTML]

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      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.

      Comment

      • kaiser0427
        New Member
        • Jan 2009
        • 6

        #4
        Originally posted by acoder
        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.
        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.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          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.

          Comment

          • kaiser0427
            New Member
            • Jan 2009
            • 6

            #6
            the first version is closer to what you're looking for.
            thanks. that gives me a direction in which to go.

            Comment

            • kaiser0427
              New Member
              • Jan 2009
              • 6

              #7
              thanks for your help. This issue is resolved.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Glad to hear it. Until next time...

                Comment

                Working...