I'm trying to make a javascript game which counts mouse click for ten seconds.
The problem is that the script ignores the onclick event somehow.Here is the code.

<html>
<head>
<script type="text/javascript">
var c=0;
var t;
var timer_is_on=0;
function timedCount()
{
document.getElementById('but').value=c;
c=c+1;
t=setTimeout("timedCount()",1000);
if (c>10)
{alert('over');
c=0;}
}

function doTimer()
{
if (!timer_is_on)
	{
	timer_is_on=1;
	timedCount();
	}
}

function broi()
{
var count=0;
document.getElementById('pole').value=count;
count=count+1;
}
document.onclick=broi;
</script>
</head>

<body>
<form>
<input type="button" value="Start" onclick="doTimer()">
<input type="text" id="but" />
<input type="text" id="pole" onmouseover="broi()" />
</form>
</body>
</html>

On line 10, it is not safe to use value from an input text as integer right away. Certain browser version may think that it is a text and would just add string '1' to it instead of an integer. Use parseInt(value, base) to fix this.

On line 19, it is not safe to expect an integer 0 as boolean. If the variable is known as integer, use integer comparison -- if (time_is_on==0) -- for safer result.

On line 28. You keep declaring a new 'count' as 0 every time your mouse is over. As a result, the count won't increase but 1.

Thanks for the advices. I've upgraded the code but it doesn't seem to work absolutely right. It counts about one half of the clicks. Here is the updated code:

<html>
<head>
<script type="text/javascript">
document.onclick = broi;
var con=0;
var c=0;
var t;
var timer_is_on=0;
function timedCount()
{
document.getElementById('but').value=c;
c=c+1;
t=setTimeout("timedCount()",1000);
if (c>10)
{alert('over');
alert(con);
con=0;
c=0;}
}

function doTimer()
{
if (timer_is_on==0)
	{
	timer_is_on=1;
	timedCount();
	}
}

function broi()
{ 
con=con+1;
}
</script>
</head>

<body>
<form>
<input type="button" value="Start" onclick="doTimer()">
<input type="text" id="but" />
<input type="text" id="pole" />
</form>
</body>
</html>

What do you mean by counted only 1/2? I tested your code (on FF), it worked fine but the timer is always looping. The setTimeout() should be in 'else' condition of if(c>10) or you will get infinite loop for time count.

Also, it is better to put con=0 and c=0 inside 'if' condition of timer_is_on==0. The reason is that you are initializing variables and it would be more intuitive to initialize at the start. It is OK to do it your way as 'reset' variables at the end, but I would rather put them in the start.

The code below is just a modification from your code.

<html>
<head>
<script type="text/javascript">
document.onclick = broi;
var con=0;
var c=0;
var t;
var timer_is_on=0;
function timedCount() {
  document.getElementById('but').value=c;
  c=c+1;
  if (c>10) {  // 10 seconds already, stop
    alert('over');
    alert(con);
    timer_is_on = 0  // reset the variable to initial state
  }
  else {  // not 10 seconds yet, keep going
    t=setTimeout("timedCount()",1000);
  }
}

function doTimer() {
  if (timer_is_on==0) {
    timer_is_on=1;  // flag to not call this again until done
    con=0;  // reset value here
    c=0;    // reset value here
    timedCount();
  }
}

function broi() {
  con=con+1;
}
</script>
</head>

<body>
<form>
<input type="button" value="Start" onclick="doTimer()">
<input type="text" id="but" />
<input type="text" id="pole" />
</form>
</body>
</html>

By the way, you know that any JavaScript game can be manipulated by users, right? In other words, JavaScript is not secure.

Thanks for the help. It doesn't count properly if you click very fast(the PC can't 'feel' the click). But I'm kind a newbie and you said Javascript is insecure.
Can you tell me how is it possible to manipulate this. Will this game be more secure if I rewrite it in Flash(Actionscript). Also i put if(timer_is_on==1) before line 32. In that way the script won't count clicks before hitting the start button.

You could also manipulate Actionscript as well. Any client side script can be manipulated by users. You should try FF (with Firebug) or Chrome browser (built-in). Then, you can edit anything or even hijack script functions. In your case, I could run the script, and then edit the count value to whatever I want before the time ends.

Also i put if(timer_is_on==1) before line 32. In that way the script won't count clicks before hitting the start button.

Yes, but it still does not help if I can directly modify the click number. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.