By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 1,973 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

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

P: 16
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.as...iming_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 function is called again?
Is there any work-around to avoid such thing?

Thanks alot
Oct 20 '08 #1
Share this Question
Share on Google+
4 Replies


iam_clint
Expert 100+
P: 1,208
You can avoid this... its not going faster.. its just firing more instances


You can use this for a toggle on/off
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var c=0;
  5. var t;
  6. var isEnabled=false;
  7. function timedCount()
  8. {
  9. if (isEnabled) {
  10. document.getElementById('txt').value=c;
  11. c=c+1;
  12. t=setTimeout("timedCount()",1000);
  13. }
  14. }
  15. </script>
  16. </head>
  17.  
  18. <body>
  19. <form>
  20. <input type="button" value="Start count!" onClick="if (isEnabled==false) { isEnabled=true; } else { isEnabled=false; } timedCount();"><input type="text" id="txt">
  21. </form>
  22. <p>Click on the button above. The input field will count for ever, starting at 0.</p>
  23. </body>
  24.  
  25. </html>
  26.  

But heres a better way of accomplishing their timer

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var c=0;
  5. var t;
  6. function timedCount()
  7. {
  8. document.getElementById('txt').value=c;
  9. c+=1;
  10. }
  11. </script>
  12. </head>
  13.  
  14. <body>
  15. <form>
  16. <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">
  17. </form>
  18. <p>Click on the button above. The input field will count for ever, starting at 0.</p>
  19. </body>
  20.  
  21. </html>
  22.  
This uses 1 timer.. that calls timedCount at an interval rather than just 1 time.
Oct 20 '08 #2

P: 16
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!
Oct 20 '08 #3

P: 6
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:

Expand|Select|Wrap|Line Numbers
  1. var c=0;
  2. var t;
  3. function timedCount() {
  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.         //if (temp.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;
  62.         //alert("you are node number: " + counter);
  63.         t=window.setInterval('timedCount()', 1000);
  64.     }
  65. }
  66.  
  67. function removeWhitespace(xml) {
  68.     var loopIndex;
  69.     for (loopIndex = 0; loopIndex < xml.childNodes.length;
  70.         loopIndex++) {
  71.  
  72.         var currentNode = xml.childNodes[loopIndex];
  73.  
  74.         if (currentNode.nodeType == 1) {
  75.         removeWhitespace(currentNode);
  76.  
  77.         if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType == 3)) {
  78.         xml.removeChild(xml.childNodes[loopIndex--]);
  79.         }
  80.       }
  81.     }
  82. }
I have also inserted the link to the page.
Untitled Document
Jan 7 '09 #4

iam_clint
Expert 100+
P: 1,208
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
Feb 2 '09 #5

Post your reply

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