onmousedown is not what I expect in JavaScript

<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.
0
RC
7/26/2006 8:29:18 PM
comp.lang.javascript 38252 articles. 0 followers. javascript4 (1315) is leader. Post Follow

6 Replies
311 Views

Similar Articles

[PageSpeed] 53

RC wrote:
> <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.

0
web
7/26/2006 9:11:14 PM
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()
}
}

-- 
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
0
Randy
7/27/2006 1:03:25 AM
Randy Webb wrote:
> 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()
> }
> }

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>



-- 
Rob

0
RobG
7/27/2006 5:42:10 AM
RC wrote:
<snip>
> So The implementation for onmousedown in JavaScript is
> different from mousePressed event in MouseListener interface
> in Java.

Richard.

0
Richard
7/27/2006 11:24:58 AM
RobG said the following on 7/27/2006 1:42 AM:
> Randy Webb wrote:
>> 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()
>> }
>> }
> 
> Wont that create a deeply recursive function? 

Absolutely <g>

> 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.

-- 
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
0
Randy
7/27/2006 9:56:00 PM
Randy Webb wrote:
> 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


> > If the mouse button is held down for some time, it will likley cause a problem.
>
> 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.


> > 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.

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  :-)


-- 
Rob

0
RobG
7/28/2006 12:26:55 AM
Reply: