Newbie - Counter script

B

Bundy

Hi




I am trying to write a simple script which counts from 1 to 4.

Problem - It shows 'Start' waits for 2 second then goes straight to 4.

I have just strarted programming so please keep it simple.

Thanks

Bundy


My script

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<script language="JavaScript" type="text/JavaScript">
var abc = 0;

function every_second()
{
for (x=1; x<5; x++)
{
setTimeout("second_function()",2000);
}
}

function second_function()
{
para1.innerText = abc;
abc++;
}

</SCRIPT>
</head>
<body onLoad="every_second();">
<p id="para1" > Start </p>
</body>
</html>
 
E

Evertjan.

Bundy wrote on 10 jan 2006 in comp.lang.javascript:
<script language="JavaScript" type="text/JavaScript">

language="JavaScript" do not use anymore!
var abc = 0;

function every_second()
{
for (x=1; x<5; x++)
{
setTimeout("second_function()",2000);
}
}

All 4 timeouts staart at nearly the same time and fire at nearly the same
time
function second_function()
{
para1.innerText = abc;

..innerText is IE only!
abc++;
}

</SCRIPT>
</head>
<body onLoad="every_second();">
<p id="para1" > Start </p>
</body>
</html>


Try:

<script type="text/JavaScript">
var abc = 0;
function myFunction(){
para1.innerHTML = abc++;
setTimeout('myFunction()',2000);
}
</script>

<body onLoad='myFunction();'>
<p id='para1'>x</p>
 
B

Bundy

Evertjan. said:
Bundy wrote on 10 jan 2006 in comp.lang.javascript:




language="JavaScript" do not use anymore!




All 4 timeouts staart at nearly the same time and fire at nearly the same
time




.innerText is IE only!




Try:

<script type="text/JavaScript">
var abc = 0;
function myFunction(){
para1.innerHTML = abc++;
setTimeout('myFunction()',2000);
}
</script>

<body onLoad='myFunction();'>
<p id='para1'>x</p>

Thanks.

To stop script counting to 4 added inside funtion
if(abc<5)
{
setTimeout('myFunction()',2000);
}
else
{
para1.innerHTML = "finishing counting to four";
return;
}

Bundy
 
L

Lee

Bundy said:
function every_second()
{
for (x=1; x<5; x++)
{
setTimeout("second_function()",2000);
}
}

When posting code, please convert TAB characters to spaces.

The setTimeout() function does not insert any delay.
It simply schedules the expression to be evaluated after the
specified delay. Once it has been scheduled (which takes
almost no time at all), processing continues. In your case,
it then immediately schedules the next invocation of your
second_function().

The method that Evertjan has posted is generally preferred,
but as an illustration, here's another way to do what you
seem to want. Each invocation is scheduled to occur two
seconds later than the previous one:

function every_second()
{
for (x=1; x<5; x++)
{
setTimeout("second_function()",2000*x);
}
}
 
E

Evertjan.

Bundy wrote on 10 jan 2006 in comp.lang.javascript:
Thanks.

To stop script counting to 4 added inside funtion
if(abc<5)
{
setTimeout('myFunction()',2000);
}
else
{
para1.innerHTML = "finishing counting to four";
return;
}

The return has no function at the end of a function.

try:

<script type="text/JavaScript">
var abc = 0;
function myFunction(){
if (abc=4){
para1.innerHTML = "finished counting at four";
return;
}
para1.innerHTML = abc++;
setTimeout('myFunction()',2000);
}
</script>

<body onLoad='myFunction();'>
<p id='para1'>x</p>
 
B

Bundy

Evertjan. said:
Bundy wrote on 10 jan 2006 in comp.lang.javascript:




The return has no function at the end of a function.

try:

<script type="text/JavaScript">
var abc = 0;
function myFunction(){
if (abc=4){
para1.innerHTML = "finished counting at four";
return;
}
para1.innerHTML = abc++;
setTimeout('myFunction()',2000);
}
</script>

<body onLoad='myFunction();'>
<p id='para1'>x</p>
One small error, in 'if(abc=0)' replace = with ==.

I prefer your code.

Thanks

Bundy
 
B

Bundy

Lee said:
Bundy said:




When posting code, please convert TAB characters to spaces.

The setTimeout() function does not insert any delay.
It simply schedules the expression to be evaluated after the
specified delay. Once it has been scheduled (which takes
almost no time at all), processing continues. In your case,
it then immediately schedules the next invocation of your
second_function().

The method that Evertjan has posted is generally preferred,
but as an illustration, here's another way to do what you
seem to want. Each invocation is scheduled to occur two
seconds later than the previous one:

function every_second()
{
for (x=1; x<5; x++)
{
setTimeout("second_function()",2000*x);
}
}

Thanks

This had me perplexed, now I understand.

Bundy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top