473,405 Members | 2,294 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,405 software developers and data experts.

settimeout problem...

Hello,

I have a problem with settimeout.
When I call the initCountDown method on body's onLoad event. If I call the initCountDown on body's load, the button's code works fine afterwards.

However, if I do not place initcountDown in body's load, the button calls countDown method only once and settimeout does not function.

Any help or tips would be greatly appreciated.
Thanks

*********************************************
Expand|Select|Wrap|Line Numbers
  1. <SCRIPT language="JavaScript">
  2. var secs;
  3. var timeout;
  4. function initCountDown(init) {
  5.     secs = init;
  6.     countDown();
  7. }
  8.  
  9. function countDown () {
  10.     if (secs >= 0) {
  11.         self.status = secs;
  12.         secs = secs - 1 ;
  13.         timeout = setTimeout(function(){countDown();}, 1000);
  14.     }  else {
  15.         clearTimeout(timeout);    
  16.     }
  17. }
  18. </SCRIPT>
  19.  
  20. <body onload="initCountDown(10)">
  21. <form>
  22. <INPUT type=submit id=btnCount value="Count Down" onClick = "initCountDown(10)">
  23. </form>
  24. </body>
Nov 16 '06 #1
1 1435
gits
5,390 Expert Mod 4TB
just add a return false; to the js-call for the button's onclick, otherwise the form is submitted (since your button is a submit-button) and the page reloaded:

Expand|Select|Wrap|Line Numbers
  1. <input type="submit" value="Count Down" onClick = "initCountDown(10); return false;">
  2.  
another option would be to use type="button" for the button :) and submit the form through javascripts submit() method.

kind regards
May 8 '08 #2

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

Similar topics

10
by: Jupiter49 | last post by:
I'm trying to move pictures around on the screen, with a slight delay between the first picture and second picture (following onmousemove). When I add the setTimeout function I must be doing it...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
29
by: Mic | last post by:
Goal: delay execution of form submit Code (Javascript + JScript ASP): <% Response.Write("<OBJECT ID='IntraLaunch' STYLE='display : none' WIDTH=0 HEIGHT=0...
4
by: Keon | last post by:
Hoi again... I trie to recall a function after 15 sec. the code I use is like this function reloads() { switch (stPage) { case 0: setTimeout("document.getElementById('myIframe').src...
3
by: Dica | last post by:
this works: document.all.style.visibility = 'hidden'; this doesn't setTimeout("document.all.style.visibility = 'hidden'",1000); looks like sResponseDivID isn't recognized. what's the problem?...
2
by: Athanasius | last post by:
Could someone shed some light as to why the following setTimeout function will not work on the Mac IE5.2? It does however work on PC(Forefox,Netscape,IE) & Mac(Safari,Firefox). Here is the script,...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
28
by: Andre | last post by:
Hi, Does anyone know whether the ECMA, or an other standard document, specifies a maximum for the value that can be pass to the setTimeOut() function in Javascript? Andre
15
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first...
7
by: Martin | last post by:
Can I have two setTimeouts running at the same time - with two different intervals? I want to start one timer and, before it times out, start another one I've tried this and they seems to...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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,...
0
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...
0
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,...
0
jinu1996
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...
0
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...

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.