By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,366 Members | 1,366 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,366 IT Pros & Developers. It's quick & easy.

onmousedown is not what I expect in JavaScript

P: n/a
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.
Jul 26 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

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.

Jul 26 '06 #2

P: n/a
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/
Jul 27 '06 #3

P: n/a

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

Jul 27 '06 #4

P: n/a
RC wrote:
<snip>
So The implementation for onmousedown in JavaScript is
different from mousePressed event in MouseListener interface
in Java.
Richard.

Jul 27 '06 #5

P: n/a
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/
Jul 27 '06 #6

P: n/a

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

Jul 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.