473,728 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

16 New Member

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:


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 4535
1,208 Recognized Expert Top Contributor
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>
  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>
  25. </html>

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>
  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>
  21. </html>
This uses 1 timer.. that calls timedCount at an interval rather than just 1 time.
Oct 20 '08 #2
16 New Member
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
6 New Member
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;
  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.         //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. }
  67. function removeWhitespace(xml) {
  68.     var loopIndex;
  69.     for (loopIndex = 0; loopIndex < xml.childNodes.length;
  70.         loopIndex++) {
  72.         var currentNode = xml.childNodes[loopIndex];
  74.         if (currentNode.nodeType == 1) {
  75.         removeWhitespace(currentNode);
  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
1,208 Recognized Expert Top Contributor
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

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 event (mouse or key) starts the timer. Then, each time the event (mouse or key) occurs, I need to display the present time in the corresponding spot on the screen (so when it's over, the screen is displaying five times for each corresponding event)....
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 it, a frontend and a backend. Case 1: If vba code on the frontend updates many rows (360,000) on the backend, a form's timer event (from the frontend) will stop firing until the user gives the form focus. (Note that the update itself always...
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 control. The service will "sleep" for 'n' seconds using the timer control and whenever the timer_elapsed event occurs, I use the performance counter object to determine availability of few resources. Based on the availability of resources, I use the...
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: ------------- ....some code... Dim usersTimers(4) As System.Windows.Forms.Timer For i = 0 To 4
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 code in the .elapsed event takes 1 secocond to complete, what happens? 1) Timer starts. 10 milliseconds later the code is executed and timer stops. When code is done, 1 seconds later, the timer continues. 10 milliseconds later the code is...
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 the default of True. The Elapsed event handler updates the dialog box with how long before it will close, acting as a timer itself. The dialog has a time to close property which is checked every time the Elapsed event fires. The problem I have...
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 javascript function which calls setTimeout and will process a second javascript function "Warn" just before the session expires. The Warn function displays an html page with a button. A second timer is started to cause the html page to close...
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 seems recently connections just drop off because the timer stops firing. My question is if there is a timeout in the timer event that just shuts down the call if the timer event is taking too long to complete...?
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 will stop executing after 30 to 40 days. After learning this, I found the same issue had been documented in the the System.Threading.Timer class as well. This limits my options for having a timer based windows service using the .net framework....
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: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.