473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
4 4506
iam_clint
1,208 Expert 1GB
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
agun
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
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
1,208 Expert 1GB
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

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

Similar topics

8
by: Victor | last post by:
I need a JavaScript timer - I have five events I need to time, that can be triggered by a mouseclick event, or a keypress event. Each event is separated by only one to two seconds. The first...
13
by: Manuel Lopez | last post by:
I have a puzzling form timer problem that I didn't experience prior to Access 2003 (though I'm not sure access 2003 is to blame). Here's the situation: a computer has two access 2003 databases on...
5
by: Dhilip Kumar | last post by:
Hi all, I have developed a windows service using the windows service project template in VS.NET. I have used three controls in the service, a timer, performance counter and a message queue...
7
by: Grahmmer | last post by:
I have a few timers that are added to a form at runtime. I can handle the event fine, but I cannot identify which timer fired. Is there a way to do this? Timer Creation: -------------...
11
by: Philip Wagenaar | last post by:
Hello, I am using a timer object in my Windows Forms Application. Does the code in ..elapsed event run in a diffrent thread? If the interval is set to 10 milliseconds and the time to execute the...
4
by: Liverpool fan | last post by:
I have a windows application written using VB .NET that encompasses a countdown timer modal dialog. The timer is a System.Timers.Timer with an interval of 1 second. AutoReset is not set so accepts...
4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
7
by: RobKinney1 | last post by:
Hello, Wow...I have one for you all and hopefully I am not understanding this timer object correctly. I have a timer setup that pulses a connection through a socket every 60 seconds. But it...
10
by: igor | last post by:
I have recently discovered that the system.Timers.Timer from.Net Framework v1.1 is not reliable when used on Windows 2003 server. When incorporated into a Windows Service, the timer_elapsed event...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.