Click to See Complete Forum and Search --> : simple counter (decrease/increase with case)
NiCkReD
May 25th, 2005, 11:16 AM
hello,
i want to make a counter. It is something strange that i want to do.
Well, i want to check 2 variables and then if one variable is bigger/lower to increase/decrease a counter.
I had make one but the max number was only 1/-1
<SCRIPT>
<!--
R1=(will be from 0-1024);
R2=(will be from 0-1024);
var R11=0;
var R22=0;
var R3=0;
if (R1<100)
R11++;
if (R2<100)
R22++;
if (R11>R22)
R3++; //my problem is here. it can't take more than 1
if (R11<R22)
R3--; //same here
document.write(R11);
document.write(R22);
document.write(R3);
// -->
</SCRIPT>
could you please help me?
Thak you in advanced :)
NiCkReD
May 25th, 2005, 11:24 AM
i forgot something!
i want to check it every 50ms if this could be possible....
Bnt
May 25th, 2005, 11:53 AM
Hi,
Firstly this is a javascript issue, not a java issue so your post should be in the scripts forum.
The reason you only ever get to 1 and -1 is that your code is only executed once, if you continued executing your code in a loop of some sort or keep refreshing the page and passing the current values each time you refresh as parameters, reinitialise your counter variables with the values passed and execute your code again - then you will get passed 1 and -1.
I don't know how you can do it every 50ms though. You could refresh your page every second, not sure how to get milliseconds.
<meta http-equiv="REFRESH" content="1">
this will refresh your page every second. Remember to pass your values to the page when you refresh.
Hope this helps
Byron
NiCkReD
May 25th, 2005, 12:10 PM
this will refresh your page every second. Remember to pass your values to the page when you refresh.
ok,it will refresh the page but i will loose the result!How and where can i pass the value?
ps. sorry but i confused. i hope the moderator to move the subject there :)
Bnt
May 26th, 2005, 05:13 AM
Hi,
Sorry I forgot you will need a server technology like JSP, ASP or PHP in order to pass and retrieve values between pages. I think you should rather ask in the scripts forum, they will be able to help you better than I have.
Byron
cjard
May 27th, 2005, 09:11 PM
i hope the moderator to move the subject there :)
your demand is my command
jw_wvu
June 3rd, 2005, 11:40 AM
I'm not a java script coder. If I understand the problem right, then you need some simple counter that is updated periodically.
Take a look at this code. You should be able to get what you need from it.
<html>
<head>
<script type = "text/javascript">
tid = 0;
count = 0;
var to;
function time()
{
tid=window.setTimeout("time()",to);
count ++;
f.result.value = count;
}
function start(x)
{
f = x;
to = 60 * 60; // change this value to change the "refresh" rate
time();
}
function cleartids() {
window.clearTimeout(tid);
}
</script>
</head>
<body onload="start(document.forms[0])" onunload="cleartids()">
<center>
<form name = "counter">
<input type = "text" name = "result" size = 8>
</body>
</html>
Dr. Script
June 3rd, 2005, 12:38 PM
This thread doesn't seem to be going anywhere. If you could try to reexplain your problem, and post your most-up-to-date code, I can help you.
The first thing, I will say you'll need to use setInterval (not setTimeout). However, both receive a fucntion as a first argument, not a string as shown in the previous example:
var i = setInterval(action,time in miiliseconds); /* same as setTimeout */
Dr. Script
NiCkReD
June 3rd, 2005, 05:49 PM
I'm not a java script coder. If I understand the problem right, then you need some simple counter that is updated periodically.
Take a look at this code. You should be able to get what you need from it.
<html>
<head>
<script type = "text/javascript">
tid = 0;
count = 0;
var to;
function time()
{
tid=window.setTimeout("time()",to);
count ++;
f.result.value = count;
}
[/
function start(x)
{
f = x;
to = 60 * 60; // change this value to change the "refresh" rate
time();
}
function cleartids() {
window.clearTimeout(tid);
}
</script>
</head>
<body onload="start(document.forms[0])" onunload="cleartids()">
<center>
<form name = "counter">
<input type = "text" name = "result" size = 8>
</body>
</html>
i want to put that if (R11>R22)
R3++; //my problem is here. it can't take more than 1
if (R11<R22)
R3--; //same here
somewhere to the code that you gave me...because that i want from javasctipt is to check 2 variables. In depent from the values i want to "play" with the R3!
to = 60 * 60; // change this value to change the "refresh" rate
the time is hour? 60*60=3600sec?
NiCkReD
June 3rd, 2005, 06:02 PM
This thread doesn't seem to be going anywhere. If you could try to reexplain your problem, and post your most-up-to-date code, I can help you.
The first thing, I will say you'll need to use setInterval (not setTimeout). However, both receive a fucntion as a first argument, not a string as shown in the previous example:
var i = setInterval(action,time in miiliseconds); /* same as setTimeout */
Dr. Script
well, as i said at the beginning, i have 2 variables. The values are from 0 to 1024.
R1=(will be from 0-1024);
R2=(will be from 0-1024);
i check them:
if (R1<100)
R11++;
if (R2<100)
R22++;
i put 2 more variables that i want to check them later....
...and i want to increase/decrase one counter.something like that
if (R11>R22)
R3++; //my problem is here. it can't take more than 1
if (R11<R22)
R3--; //same here
the time that i want to execute those commands are in msec. Also i want to view the value of R3 at the broswer.
NiCkReD
June 5th, 2005, 04:04 PM
well i made that
function time()
{
tid=window.setTimeout("time()",to);
if (R1<100)
R11++;
if (R2<100)
R22++;
if (R11>R22){
count ++;
f.result.value = count;}
if (R11<R22){
count --;
f.result.value = count;}
}
it works somehow...
when tha page load, it checks if (R1<100) . If it is true, then the count begin to increase without stop!
i want to increase only one time and then recheck the two condiotions.
is any way?
jw_wvu
June 7th, 2005, 08:25 AM
I'm having a little trouble following what you need.
Why don't you post what you have done already.
NiCkReD
June 8th, 2005, 06:18 PM
that is the page that i have
<html>
<head>
<script type = "text/javascript">
<!--
tid = 0;
count = 0;
R1=( );
R2=( );
var R11=0;
var R22=0;
var to;
function time()
{
tid=window.setTimeout("time()",to);
if (R1<100)
R11++;
if (R2<100)
R22++;
if (R11>R22){
count ++;
f.result.value = count;}
if (R11<R22){
count --;
f.result.value = count;}
}
function start(x)
{
f = x;
to = 600; // change this value to change the "refresh" rate
time();
}
function cleartids() {
window.clearTimeout(tid);
}
// -->
</script>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=windows-1253">
</head>
<body onload="start(document.forms[0]);" onunload="cleartids();">
<p align="center"><center>
<form name = "counter"></p>
</body>
</html>
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.