473,666 Members | 2,354 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
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 4525
iam_clint
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>
  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 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
kaiser0427
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;
  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 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

8
6194
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)....
13
7477
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...
5
5085
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...
7
2143
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
11
7550
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...
4
11111
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...
4
5226
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...
7
6021
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...?
10
4278
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....
0
8355
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8638
tracyyun
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...
0
7381
agi2029
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...
0
4193
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...
0
4365
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.