onmousedown is not what I expect in JavaScript

R

RC

<a href="#" onmousedown="callMyFunction()">Press the mouse button</a>

What I expect is when I or an user move mouse pointer over that link,
and press the mouse button. Then will "callMyFunction()" over and
over again until I release the mouse button (mouseup).

But what I found out the onmousedown is just acts the same as onclick.
It only "callMyFunction" once. If I want to call again, I need to
keep repeat mousedown, mouseup like clicking. That is not cool at all!

So The implementation for onmousedown in JavaScript is different from
mousePressed event in MouseListener interface in Java.
 
W

web.dev

RC said:
<a href="#" onmousedown="callMyFunction()">Press the mouse button</a>

What I expect is when I or an user move mouse pointer over that link,
and press the mouse button. Then will "callMyFunction()" over and
over again until I release the mouse button (mouseup).

But what I found out the onmousedown is just acts the same as onclick.
It only "callMyFunction" once. If I want to call again, I need to
keep repeat mousedown, mouseup like clicking. That is not cool at all!

Why not implement it so that once they click, your function will
continuously be called. However, on an onmouseout event, you can make
it so that the "callMyFunction" function stops being called.
So The implementation for onmousedown in JavaScript is different from
mousePressed event in MouseListener interface in Java.

Probably, since Java != JavaScript.
 
R

Randy Webb

RC said the following on 7/26/2006 4:29 PM:
<a href="#" onmousedown="callMyFunction()">Press the mouse button</a>

What I expect is when I or an user move mouse pointer over that link,
and press the mouse button. Then will "callMyFunction()" over and
over again until I release the mouse button (mouseup).

But what I found out the onmousedown is just acts the same as onclick.
It only "callMyFunction" once. If I want to call again, I need to
keep repeat mousedown, mouseup like clicking. That is not cool at all!

So The implementation for onmousedown in JavaScript is different from
mousePressed event in MouseListener interface in Java.

onmousedown="callMyFunction()"
onmouseup="var keepCallingIt = false"
onclick="return false"


var keepCallingIt = true;
function callMyFunction(){
//code here for callMyFunction
if(keepCallingIt){
callMyFunction()
}
}
 
R

RobG

Randy said:
RC said the following on 7/26/2006 4:29 PM:

onmousedown="callMyFunction()"
onmouseup="var keepCallingIt = false"
onclick="return false"


var keepCallingIt = true;
function callMyFunction(){
//code here for callMyFunction
if(keepCallingIt){
callMyFunction()
}
}

Wont that create a deeply recursive function? If the mouse button is
held down for some time, it will likley cause a problem.

It may be better to use setTimeout onmousedown, then cancel it
onmouseup, e.g.:

<script type="text/javascript">

var pageCounter = (function(){
var isRunning;
return {
start : function(){
var d = document.getElementById('xx');
d.innerHTML = +d.innerHTML + 1;
isRunning = setTimeout('pageCounter.start();', 100);
},
stop : function (){
if (isRunning) clearTimeout(isRunning);
}
}
})();

</script>

<a href="#"
onmousedown="pageCounter.start();"
onmouseup="pageCounter.stop();"
start/stop</a>
<div id="xx">0</div>
 
R

Randy Webb

RobG said the following on 7/27/2006 1:42 AM:
Wont that create a deeply recursive function?

Absolutely said:
If the mouse button is held down for some time, it will likley cause a problem.

It's not "likely", rather its pretty dependable.
It may be better to use setTimeout onmousedown, then cancel it
onmouseup, e.g.:

Depending on what you want. At 13ms timeout, you can only execute it
once every 13ms, whereas with the recursion it happens as fast as the
processor can process the script.
 
R

RobG

Randy said:
RobG said the following on 7/27/2006 1:42 AM: [...]
Wont that create a deeply recursive function?

Absolutely <g>

Firefox seems to allow 1,000 recursions before a 'too much recursion'
error occurs. Maybe there's a setting somewhere to change that... ;-p

It's not "likely", rather its pretty dependable.

And pretty quick - a simple script does it in less than 10ms.

It seems to me that whatever is kicked-off by the mousedown event hogs
the one and only JS thread (in Firefox and IE at least), so the mouseup
event is qued and can't interup the process started by the mousedown.

To 'work', the script must come up for air to see if some other event
wants to do something, setTimeout (or maybe setInterval) seem to be the
only way to do that.

Depending on what you want. At 13ms timeout, you can only execute it
once every 13ms, whereas with the recursion it happens as fast as the
processor can process the script.

If the minimum interval is 13ms (or whatever, I guess it's
implementation dependent) then that is as fast as you can do it. Other
choices are to either wait for a 'too much recursion' error or a 'this
script is taking too much time' message.

Maybe they suit the OP better :)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top