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
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 -
<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 -
<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.
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!
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: - 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 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: 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,...
|
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...
| |
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,...
|
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: 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: 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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |