Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Newbie - Counter script

Reply
Thread Tools

Newbie - Counter script

 
 
Bundy
Guest
Posts: n/a
 
      01-10-2006
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>
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      01-10-2006
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>




--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
 
Bundy
Guest
Posts: n/a
 
      01-10-2006
Evertjan. wrote:
> 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>
>
>
>
>


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
 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      01-10-2006
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);
}
}

 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      01-10-2006
Bundy wrote on 10 jan 2006 in comp.lang.javascript:

>> 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;
> }
>


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>



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Bundy
Guest
Posts: n/a
 
      01-11-2006
Evertjan. wrote:
> Bundy wrote on 10 jan 2006 in comp.lang.javascript:
>
>
>>>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;
>> }
>>

>
>
> 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
 
Reply With Quote
 
Bundy
Guest
Posts: n/a
 
      01-11-2006
Lee wrote:
> 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);
> }
> }
>


Thanks

This had me perplexed, now I understand.

Bundy
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Page File counter and Private Bytes Counter George2 C++ 1 01-31-2008 09:27 AM
Running a CGI Counter Script Mark Hobley HTML 10 12-09-2005 08:51 AM
Session("counter") vs. ViewState("counter")...a newbie question The Eeediot ASP .Net 3 12-22-2004 09:31 PM
word counter script locks up Mozilla Randy Webb Javascript 6 02-16-2004 07:38 AM
Problem with counter CGI script T Stewart Perl Misc 1 07-06-2003 11:17 PM



Advertisments