Does timer in javascript getting faster if the event occurs again?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • agun
    New Member
    • Oct 2008
    • 16

    Does timer in javascript getting faster if the event occurs again?

    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:

    The W3Schools online code editor allows you to edit code and view the result in your browser


    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 function is called again?
    Is there any work-around to avoid such thing?

    Thanks alot
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    You can avoid this... its not going faster.. its just firing more instances


    You can use this for a toggle on/off
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var c=0;
    var t;
    var isEnabled=false;
    function timedCount()
    {
    if (isEnabled) {
    document.getElementById('txt').value=c;
    c=c+1;
    t=setTimeout("timedCount()",1000);
    }
    }
    </script>
    </head>
    
    <body>
    <form>
    <input type="button" value="Start count!" onClick="if (isEnabled==false) { isEnabled=true; } else { isEnabled=false; } timedCount();"><input type="text" id="txt">
    </form>
    <p>Click on the button above. The input field will count for ever, starting at 0.</p>
    </body>
    
    </html>

    But heres a better way of accomplishing their timer

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var c=0;
    var t;
    function timedCount()
    {
    document.getElementById('txt').value=c;
    c+=1;
    }
    </script>
    </head>
    
    <body>
    <form>
    <input type="button" value="Start count!" onClick="t=window.setInterval('timedCount()', 1000);"><input type="button" value="Stop" onClick="window.clearInterval(t);"><input type="text" id="txt">
    </form>
    <p>Click on the button above. The input field will count for ever, starting at 0.</p>
    </body>
    
    </html>
    This uses 1 timer.. that calls timedCount at an interval rather than just 1 time.

    Comment

    • agun
      New Member
      • Oct 2008
      • 16

      #3
      thanks clint, it works

      i'm making a ticker tape script for stock quotes that can refresh automatically every several minutes and just meet this problem, cause everytime it refreshes, it becomes faster (firing multiple instances that is)

      thanks! you're very helpful!

      Comment

      • kaiser0427
        New Member
        • Jan 2009
        • 6

        #4
        trouble with timer on loop

        Hi, I ran across this thread and am having a similar problem. I am trying to get this code to execute every (x) seconds. I placed an alert in to get it to stop after it writes each node and that works. When I put this timer in it doesn't pause. Plus I am working on the if statement that is commented out too so any help on that would be appreciated. Here is the code:

        Code:
        var c=0;
        var t;
        function timedCount() {
        	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++) {
        		//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;
        		//alert("you are node number: " + counter);
        		t=window.setInterval('timedCount()', 1000);
        	}
        }
        
        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--]);
        		}
        	  }
        	}
        }
        I have also inserted the link to the page.
        Untitled Document

        Comment

        • iam_clint
          Recognized Expert Top Contributor
          • Jul 2006
          • 1207

          #5
          I looked at http://davispubs.com/westbycoc/xmltest.html
          looks fine? you still having the issue? granted it took me a month to look at this thread

          Comment

          Working...