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

Why is setTimeout() not firing?

7
Hi,
I am not sure why setTimer is not working for me.

I have the folowing fucntion before </head> and included few document.write to make sure it is working.
Expand|Select|Wrap|Line Numbers
  1. function adding(x1){
  2.             document.write("adding"+"<br/>");
  3.             document.write(" x1="+x1);
  4.             var x2=x1;
  5.                 document.write(" x2="+x2);
  6.  
  7.             if (x2 <= 10){
  8.                 x2=x2+1;
  9.                 document.write(" x2="+x2);
  10.                 setTimeout("adding(x2)", 300);
  11.             }else
  12.                 { document.write("bye");
  13.                 } 
  14.         }
  15.  
Then I have the following after <body>
Expand|Select|Wrap|Line Numbers
  1. var x1=1;
  2.         document.write("x1="+x1);
  3.         adding(x1);
Sep 22 '07 #1
4 3138
The reason why the function is not working is because you are using document.write. Once that is called after the pages loads, the entire page is rewritten. Because of this, you will only see the increase once. I would recommend using alert() to make sure a function is working. In addition, instead of using
Expand|Select|Wrap|Line Numbers
  1. setTimeout("adding(x2)", 300);
  2.  
Use this:
Expand|Select|Wrap|Line Numbers
  1. setTimeout("adding("+x2+")", 300);
  2.  
I hope I helped.
Sep 22 '07 #2
pbmods
5,821 Expert 4TB
Heya, aayybb. Welcome to TSDN!

Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

Please use CODE tags when posting source code:

[CODE=javascript]
JavaScript code goes here.
[/CODE]
Sep 22 '07 #3
aayybb
7
Thank you very much. It works.



The reason why the function is not working is because you are using document.write. Once that is called after the pages loads, the entire page is rewritten. Because of this, you will only see the increase once. I would recommend using alert() to make sure a function is working. In addition, instead of using
Expand|Select|Wrap|Line Numbers
  1. setTimeout("adding(x2)", 300);
  2.  
Use this:
Expand|Select|Wrap|Line Numbers
  1. setTimeout("adding("+x2+")", 300);
  2.  
I hope I helped.
Sep 22 '07 #4
aayybb
7
Sorry. I will post a better message next time.



Heya, aayybb. Welcome to TSDN!

Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

Please use CODE tags when posting source code:

Expand|Select|Wrap|Line Numbers
  1. JavaScript code goes here.
  2.  
Sep 22 '07 #5

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) {...
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,...
6
by: Brent | last post by:
Is there any obvious reason why there's no delay in the execution of setting the id.style.display when this function gets called? function Hide(divId,timeout) { var id =...
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
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...
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
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,...
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...
0
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...
0
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,...

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.